27 lines
971 B
Text
27 lines
971 B
Text
do --[[ find elements of the Smarandache prime-digital sequence
|
|
- primes whose digits are all primes
|
|
Translated from the Lua sample, extending to the 1000th Smarandache prime
|
|
]]
|
|
|
|
-- SIEVE: sieve the primes to 5 000 000
|
|
local sieve, S = {}, 5_000_000
|
|
for i = 2,S do sieve[i]=true end
|
|
for i = 2,S do
|
|
if sieve[i] then
|
|
for j=i*i,S,i do sieve[j]=nil end
|
|
end
|
|
end
|
|
|
|
-- TASKS:
|
|
local digs, cans, spds, N = {2,3,5,7}, {0}, {}, 1000
|
|
-- the first trip through the following loop adds the single digit primes to cans
|
|
-- subsequent trips add the previous candidates * 10 + the final prime digits
|
|
while #spds < N do
|
|
local c <const> = cans:remove(1)
|
|
for _,d in ipairs(digs) do cans:insert(c*10+d) end
|
|
if sieve[c] then spds:insert(c) end
|
|
end
|
|
print(" 1-25: " .. spds:slice(1,25):concat(" "))
|
|
print(" 100th: " .. spds[ 100])
|
|
print("1000th: " .. spds[1000])
|
|
end
|