-
Notifications
You must be signed in to change notification settings - Fork 6
/
026 Reciprocal cycles.jl
85 lines (58 loc) · 1.26 KB
/
026 Reciprocal cycles.jl
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
84
85
#!/usr/bin/julia
# Daniel "Trizen" Șuteu
# Edit: 25 July 2021
# https://github.com/trizen
# Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
# https://projecteuler.net/problem=26
# Runtime: 1.014s
using Primes
function divisors(n)
d = Int64[1]
for (p,e) in factor(n)
t = Int64[]
r = 1
for i in 1:e
r *= p
for u in d
push!(t, u*r)
end
end
append!(d, t)
end
return sort(d)
end
function znorder(a, n)
if isprime(n)
for d in divisors(n-1)
if (powermod(a, d, n) == 1)
return d
end
end
end
f = factor(n)
if (length(f) == 1) # is prime power
p = first(first(f))
z = znorder(a, p)
while (powermod(a, z, n) != 1)
z *= p
end
return z
end
pp_orders = Int64[]
for (p,e) in f
push!(pp_orders, znorder(a, p^e))
end
return lcm(pp_orders)
end
function p_26()
d,m = 1,0
for n in 2:1000
gcd(10, n) == 1 || continue
r = znorder(10, n)
if (r > m)
m,d = r,n
end
end
return d
end
println(p_26())