67 lines
2 KiB
Text
67 lines
2 KiB
Text
with javascript_semantics
|
|
atom t0 = time()
|
|
function bump(sequence half)
|
|
-- add a digit to valid halves
|
|
-- eg {0} --> {1..9} (no zeroes)
|
|
-- --> {11..99} ("")
|
|
-- --> {111..999}, etc
|
|
sequence res = {}
|
|
for i=1 to length(half) do
|
|
integer hi = half[i]*10
|
|
for digit=1 to 9 do
|
|
res &= hi+digit
|
|
end for
|
|
end for
|
|
return res
|
|
end function
|
|
|
|
procedure cyclops(string s="")
|
|
sequence res = {},
|
|
half = {0} -- valid digits, see bump()
|
|
integer left = 1, -- half[] before the 0
|
|
right = 0, -- half[] after the 0
|
|
hlen = 1, -- length(half)
|
|
cpow = 10, -- cyclops power (of 10)
|
|
bcpow = 1, -- blind cyclops power
|
|
cn = 0 -- cyclops number (scratch)
|
|
bool valid = false,
|
|
bPrime = match("prime",s),
|
|
bBlind = match("blind",s),
|
|
bPalin = match("palindromic",s)
|
|
while length(res)<50 or cn<=1e7 or not valid do
|
|
right += 1
|
|
if right>hlen then
|
|
right = 1
|
|
left += 1
|
|
if left>hlen then
|
|
half = bump(half)
|
|
hlen = length(half)
|
|
cpow *= 10
|
|
bcpow *= 10
|
|
left = 1
|
|
end if
|
|
end if
|
|
integer lh = half[left],
|
|
rh = half[right]
|
|
cn = lh*cpow+rh -- cyclops number
|
|
valid = not bPrime or is_prime(cn)
|
|
if valid and bBlind then
|
|
valid = is_prime(lh*bcpow+rh)
|
|
end if
|
|
if valid and bPalin then
|
|
valid = sprintf("%d",lh) == reverse(sprintf("%d",rh))
|
|
end if
|
|
if valid then
|
|
res = append(res,sprintf("%7d",cn))
|
|
end if
|
|
end while
|
|
printf(1,"First 50 %scyclops numbers:\n%s\n",{s,join_by(res[1..50],1,10)})
|
|
printf(1,"First %scyclops number > 10,000,000: %s at (one based) index: %d\n\n",
|
|
{s,res[$],length(res)})
|
|
end procedure
|
|
|
|
cyclops()
|
|
cyclops("prime ")
|
|
cyclops("blind prime ")
|
|
cyclops("palindromic prime ")
|
|
?elapsed(time()-t0)
|