Skip to content

Commit d03715e

Browse files
committed
solve PE - 64
1 parent bebc77e commit d03715e

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,5 @@ dmypy.json
131131
*.cmi
132132
*.cmx
133133
*.o
134+
*.cmo
134135
a.out

_064.ml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module TupSet = Set.Make(struct
2+
type t = float * float * float
3+
let compare (a,b,c) (a',b',c') =
4+
let s = string_of_float a ^ string_of_float b ^ string_of_float c in
5+
let s' = string_of_float a' ^ string_of_float b' ^ string_of_float c' in
6+
compare s s'
7+
end)
8+
9+
(* HACK: :( *)
10+
let round x = floor (x +. 0.1)
11+
12+
let is_square n = n = floor @@ sqrt n *. (floor @@ sqrt n)
13+
14+
let next x n d =
15+
let y = ((x *. x) -. (d *. d)) /. n in
16+
let z = floor (n /. (x -. d)) in
17+
(z, round (z *. y -. d), round y)
18+
19+
let sqrt_period x =
20+
let i = floor @@ sqrt x in
21+
let x = sqrt x in
22+
let (a,b,c) = next x 1.0 i in
23+
let rec aux (n,d) xs s =
24+
let (a,b,c) = next x n d in
25+
if TupSet.mem (a,b,c) s
26+
then List.rev xs
27+
else aux (c,b) ((a,b,c)::xs) (TupSet.add (a,b,c) s)
28+
in
29+
let s = TupSet.empty in
30+
let s = TupSet.add (a,b,c) s in
31+
aux (c,b) [(a,b,c)] s;;
32+
33+
let period n = List.length @@ sqrt_period n
34+
35+
let rec range n m =
36+
if n = m
37+
then [n]
38+
else n::(range (n +. 1.0) m) ;;
39+
40+
range 2.0 10000.0
41+
|> List.filter (fun n -> not (is_square n))
42+
|> List.map period
43+
|> List.filter (fun n -> n mod 2 == 1)
44+
|> List.length
45+
|> print_int
46+
|> print_newline ;;

0 commit comments

Comments
 (0)