-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquarefree_omega_palindromes.jl
105 lines (81 loc) · 2.18 KB
/
squarefree_omega_palindromes.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
#!/usr/bin/julia
# Smallest squarefree palindrome with exactly n distinct palindromic prime factors.
# See also:
# http://www.worldofnumbers.com/assign3.htm
# https://oeis.org/A046379
# Known terms:
# 1, 2, 6, 66, 6666, 334826628433, 15710977901751, 329443151344923
# It took 5min, 39,015 ms to find a(5).
# It took 5min, 27,260 ms to find a(6).
# It took 3min, 57,612 ms to find a(7).
using Primes
function big_prod(arr)
#r = big"1"
r = 1
for n in (arr)
r *= n
end
return r
end
function squarefree_omega_palindromes(A, B, n::Int64)
A = max(A, big_prod(primes(prime(n))))
F = function(m, p::Int64, j::Int64)
lst = []
s = round(Int64, fld(B, m)^(1/j))
if (j == 1)
q = nextprime(max(p, cld(A, m)))
while (q <= s)
if (reverse(string(q)) != string(q))
q = nextprime(q+1)
continue
end
if (q == 5 && m%2 == 0)
q = nextprime(q+1)
continue
end
v = m*q
if (reverse(string(v)) == string(v))
println("Found upper-bound: ", v)
push!(lst, v)
end
q = nextprime(q+1)
end
else
q = nextprime(p)
while (q <= s)
if (reverse(string(q)) != string(q))
q = nextprime(q+1)
continue
end
if (q == 5 && m%2 == 0)
q = nextprime(q+1)
continue
end
lst = vcat(lst, F(m*q, q+1, j-1))
q = nextprime(q+1)
end
end
return lst
end
#return sort(F(big"1",2,n))
return sort(F(1,2,n))
end
function a(n::Int64)
if (n == 0)
return 1
end
x = big_prod(primes(prime(n)))
y = 2*x
while (true)
println("Sieving range: ", [x,y]);
v = squarefree_omega_palindromes(x, y, n)
if (length(v) > 0)
return v[1]
end
x = y+1
y = 2*x
end
end
for n in 1:17
println([n, a(n)])
end