-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb049.jl
163 lines (145 loc) · 5.31 KB
/
pb049.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
##############################################################################
### From Primes.jl
##############################################################################
const wheel = [4, 2, 4, 2, 4, 6, 2, 6]
const wheel_primes = [7, 11, 13, 17, 19, 23, 29, 31]
const wheel_indices = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7]
@inline function wheel_index(n)
d, r = divrem(n - 1, 30)
return 8d + wheel_indices[r+2]
end
@inline function wheel_prime(n)
d, r = (n - 1) >>> 3, (n - 1) & 7
return 30d + wheel_primes[r+1]
end
function _primesmask(limit::Int)
limit < 7 && throw(ArgumentError("The condition limit >= 7 must be met."))
n = wheel_index(limit)
m = wheel_prime(n)
sieve = ones(Bool, n)
@inbounds for i = 1:wheel_index(isqrt(limit))
if sieve[i]
p = wheel_prime(i)
q = p^2
j = (i - 1) & 7 + 1
while q <= m
sieve[wheel_index(q)] = false
q += wheel[j] * p
j = j & 7 + 1
end
end
end
return sieve
end
function _primesmask(lo::Int, hi::Int)
7 <= lo <= hi || throw(ArgumentError("The condition 7 <= lo <= hi must be met."))
lo == 7 && return _primesmask(hi)
wlo, whi = wheel_index(lo - 1), wheel_index(hi)
m = wheel_prime(whi)
sieve = ones(Bool, whi - wlo)
hi < 49 && return sieve
small_sieve = _primesmask(isqrt(hi))
@inbounds for i = 1:length(small_sieve) # don't use eachindex here
if small_sieve[i]
p = wheel_prime(i)
j = wheel_index(2 * div(lo - p - 1, 2p) + 1)
r = widemul(p, wheel_prime(j + 1))
r > m && continue # use widemul to avoid r <= m caused by overflow
j = j & 7 + 1
q = Int(r)
# q < 0 indicates overflow when incrementing q inside loop
while 0 <= q <= m
sieve[wheel_index(q)-wlo] = false
q += wheel[j] * p
j = j & 7 + 1
end
end
end
return sieve
end
"""
primesmask([lo,] hi)
Returns a prime sieve, as a `BitArray`, of the positive integers (from `lo`, if specified)
up to `hi`. Useful when working with either primes or composite numbers.
"""
function primesmask(lo::Int, hi::Int)
0 < lo <= hi || throw(ArgumentError("The condition 0 < lo <= hi must be met."))
sieve = falses(hi - lo + 1)
lo <= 2 <= hi && (sieve[3-lo] = true)
lo <= 3 <= hi && (sieve[4-lo] = true)
lo <= 5 <= hi && (sieve[6-lo] = true)
hi < 7 && return sieve
wheel_sieve = _primesmask(max(7, lo), hi)
lsi = lo - 1
lwi = wheel_index(lsi)
@inbounds for i = 1:length(wheel_sieve) # don't use eachindex here
sieve[wheel_prime(i + lwi)-lsi] = wheel_sieve[i]
end
return sieve
end
primesmask(lo::Integer, hi::Integer) = lo <= hi <= typemax(Int) ? primesmask(Int(lo), Int(hi)) :
throw(ArgumentError("Both endpoints of the interval to sieve must be <= $(typemax(Int)), got $lo and $hi."))
primesmask(limit::Int) = primesmask(1, limit)
primesmask(n::Integer) = n <= typemax(Int) ? primesmask(Int(n)) :
throw(ArgumentError("Requested number of primes must be <= $(typemax(Int)), got $n."))
"""
primes([lo,] hi)
Returns a collection of the prime numbers (from `lo`, if specified) up to `hi`.
"""
function primes(lo::Int, hi::Int)
lo <= hi || throw(ArgumentError("The condition lo <= hi must be met."))
list = Int[]
lo <= 2 <= hi && push!(list, 2)
lo <= 3 <= hi && push!(list, 3)
lo <= 5 <= hi && push!(list, 5)
hi < 7 && return list
lo = max(2, lo)
sizehint!(list, 5 + floor(Int, hi / (log(hi) - 1.12) - lo / (log(lo) - 1.12 * (lo > 7)))) # http://projecteuclid.org/euclid.rmjm/1181070157
sieve = _primesmask(max(7, lo), hi)
lwi = wheel_index(lo - 1)
@inbounds for i = 1:length(sieve) # don't use eachindex here
sieve[i] && push!(list, wheel_prime(i + lwi))
end
return list
end
primes(n::Int) = primes(1, n)
##############################################################################
@inline function concat(xs...; base::T=10) where {T<:Integer}
return foldl((n, x) -> n * base^ndigits(x) + x, xs; init=zero(T))
end
function problem049(N::Integer, K::Integer)
primePermutations = Dict{Vector{Int},Vector{Int}}()
for p in primes(10^6)
k = sort(digits(p))
push!(get!(primePermutations, k, Int[]), p)
end
vals = Tuple{Int,Int128}[]
for v in values(primePermutations)
length(v) < K && continue
sort!(v)
if K == 3
for (i, p) in enumerate(v)
p < N || continue
for q in v[i+1:end]
r = 2q - p
r in v && push!(vals, (p, concat(p, q, r; base=Int128(10))))
end
end
else
for (i, p) in enumerate(v)
p < N || continue
for q in v[i+1:end]
r = 2q - p
s = 2r - q
r in v && s in v && push!(vals, (p, concat(p, q, r, s; base=Int128(10))))
end
end
end
end
sort!(vals)
for (_, t) in vals
println(t)
end
end
(N, K) = Tuple(parse(Int, n) for n in split(readline(), " "))
problem049(N, K)