40 lines
1.2 KiB
Text
40 lines
1.2 KiB
Text
local int = require "int"
|
|
local fmt = require "fmt"
|
|
|
|
local limit <const> = 999_999
|
|
local c = int.arecomps(limit)
|
|
local left_found = false
|
|
local right_found = false
|
|
print("Largest truncatable primes less than a million:")
|
|
for i = limit, 4, -2 do
|
|
if !c[i] then
|
|
if !right_found then
|
|
local p = i // 10
|
|
while p > 0 do
|
|
if p % 2 == 0 or c[p] then break end
|
|
p //= 10
|
|
end
|
|
if p == 0 then
|
|
print($" Right truncatable prime = {fmt.int(i)}")
|
|
right_found = true
|
|
if left_found then return end
|
|
end
|
|
end
|
|
if !left_found then
|
|
local q = tostring(i):sub(2)
|
|
if !("0" in q) then
|
|
local p = tonumber(q)
|
|
while #q > 0 do
|
|
if p % 2 == 0 or c[p] then break end
|
|
q = q:sub(2)
|
|
p = tonumber(q)
|
|
end
|
|
if q == "" then
|
|
print($" Left truncatable prime = {fmt.int(i)}")
|
|
left_found = true
|
|
if right_found then return end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|