RosettaCodeData/Task/Cyclops-numbers/Pluto/cyclops-numbers.pluto
2026-04-30 12:34:36 -04:00

63 lines
1.9 KiB
Text

local int = require "int"
local fmt = require "fmt"
require "table2"
local function find_first(list)
for i, n in list do
if n > 1e7 then return n, i end
end
end
local ranges = {
range(0, 0),
range(101, 909),
range(11011, 99099),
range(1110111, 9990999),
range(111101111, 119101111)
}
local cyclops = {}
for ranges as r do
local num_digits = #tostring(r[1])
local center = num_digits // 2
for r as i do
local digits = int.digits(i, 10. false, false)
if digits[center + 1] == 0 and digits:count(|d| -> d == 0) == 1 then
cyclops:insert(i)
end
end
end
print("The first 50 cyclops numbers are:")
local candidates = cyclops:slice(1, 50)
local n, i = find_first(cyclops)
fmt.tprint("%,6s", candidates, 10)
fmt.print("\nFirst such number > 10 million is %,s at one-based index %,s", n, i)
print("\n\nThe first 50 prime cyclops numbers are:")
local primes = cyclops:filter(|c| -> int.isprime(c)):reorder()
candidates = primes:slice(1, 50)
n, i = find_first(primes)
fmt.tprint("%,6s", candidates, 10)
fmt.print("\nFirst such number > 10 million is %,s at one-based index %,s", n, i)
print("\n\nThe first 50 blind prime cyclops numbers are:")
local bpcyclops = {}
local ppcyclops = {}
for primes as p do
local ps = tostring(p)
local num_digits = #ps
local center = num_digits // 2
local no_middle = tonumber(ps:sub(1, center) .. ps:sub(center + 2))
if int.isprime(no_middle) then bpcyclops:insert(p) end
if ps == ps:reverse() then ppcyclops:insert(p) end
end
candidates = bpcyclops:slice(1, 50)
n, i = find_first(bpcyclops)
fmt.tprint("%,6s", candidates, 10)
fmt.print("\nFirst such number > 10 million is %,s at one-based index %,s", n, i)
print("\n\nThe first 50 palindromic prime cyclops numbers are:")
candidates = ppcyclops:slice(1, 50)
n, i = find_first(ppcyclops)
fmt.tprint("%,9s", candidates, 8)
fmt.print("\nFirst such number > 10 million is %,s at one-based index %,s", n, i)