-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb029.jl
46 lines (35 loc) · 895 Bytes
/
pb029.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
module Problem029
using ..ProjectEulerLibrary
"""
problem029()
Problem 029 of Project Euler.
https://projecteuler.net/problem=029
https://projecteuler.net/thread=29&page=3#6162
Counting duplicates directly. https://projecteuler.net/thread=29;page=3#6162
"""
function problem029(N::Integer=100)
powerdups = zeros(Int, ilog(2, N))
for p in 2:ilog(2, N)
powers = falses(N)
for q in 1:p-1
s = lcm(p, q) ÷ p
powers[s:s:N*q÷p] .= true
end
powers[1] = false
powerdups[p] = count(powers)
end
dups = 0
powerof = falses(N)
for n in 2:isqrt(N)
powerof[n] && continue
for p in 2:ilog(n, N)
powerof[n^p] = true
dups += powerdups[p]
end
end
return (N - 1)^2 - dups
end
export problem029
end # module Problem029
using .Problem029
export problem029