-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_069.ml
More file actions
83 lines (73 loc) · 1.57 KB
/
_069.ml
File metadata and controls
83 lines (73 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
let _N = 1000007;;
let numbers = Array.make _N true ;;
numbers.(0) <- false ;;
numbers.(1) <- false ;;
let rec seive n =
if n = _N
then ()
else if numbers.(n)
then begin
let rec aux m =
if m * n >= _N then ()
else begin
(numbers.(m * n) <- false);
aux (m + 1)
end
in
aux 2;
seive (n + 1);
end
else seive (n + 1) ;;
let rec collect n xs =
if n = _N then List.rev xs
else if numbers.(n) then collect (n + 1) (n :: xs)
else collect (n + 1) xs ;;
seive 2;;
let primes = collect 2 [];;
let rec pow x n =
if n = 1 then x
else if n = 0 then 1
else if n mod 2 = 0
then let xn = pow x (n / 2) in xn * xn
else x * pow x (n - 1)
let factorize n =
let rec foo n p t =
if n mod p = 0
then foo (n / p) p (t + 1)
else t, n
in
let rec aux n ps =
if numbers.(n)
then [(n,1)]
else if n = 1 then []
else
match ps with
[] -> []
| p :: ps ->
if n mod p = 0
then
let t,n = foo n p 0 in
(p,t)::aux n ps
else aux n ps
in
let r = aux n primes in
r ;;
let phi n =
let factors = factorize n in
let rec aux fs r =
match fs with
[] -> r
| (p,n) :: fs -> aux fs (r * (pow p (n - 1)) * (p - 1))
in
aux factors 1
;;
let max_n_by_phi = ref (0.0, -1) ;;
for n = 2 to (_N - 7) do
let p = phi n in
let np = (float_of_int n) /. (float_of_int p) in
let mnp, _ = !max_n_by_phi in
if np > mnp then max_n_by_phi := (np, n) else ()
done
let mnp,max_n = !max_n_by_phi ;;
print_int max_n ;;
print_newline () ;;