How to resolve the algorithm Unprimeable numbers step by step in the Lua programming language
How to resolve the algorithm Unprimeable numbers step by step in the Lua programming language
Table of Contents
Problem Statement
As used here, all unprimeable numbers (positive integers) are always expressed in base ten.
───── Definition from OEIS ─────: Unprimeable numbers are composite numbers that always remain composite when a single decimal digit of the number is changed.
───── Definition from Wiktionary (referenced from Adam Spencer's book) ─────: (arithmetic) that cannot be turned into a prime number by changing just one of its digits to any other digit. (sic)
Unprimeable numbers are also spelled: unprimable. All one─ and two─digit numbers can be turned into primes by changing a single decimal digit.
190 isn't unprimeable, because by changing the zero digit into a three yields 193, which is a prime.
The number 200 is unprimeable, since none of the numbers 201, 202, 203, ··· 209 are prime, and all the other numbers obtained by changing a single digit to produce 100, 300, 400, ··· 900, or 210, 220, 230, ··· 290 which are all even.
It is valid to change 189 into 089 by changing the 1 (one) into a 0 (zero), which then the leading zero can be removed, and then treated as if the "new" number is 89.
Show all output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Unprimeable numbers step by step in the Lua programming language
Source code in the lua programming language
-- FUNCS:
local function T(t) return setmetatable(t, {__index=table}) end
table.filter = function(t,f) local s=T{} for _,v in ipairs(t) do if f(v) then s[#s+1]=v end end return s end
table.firstn = function(t,n) local s=T{} n=n>#t and #t or n for i = 1,n do s[i]=t[i] end return s end
-- SIEVE:
local sieve, S = {}, 10000000
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
-- UNPRIMABLE:
local unprimables, lowests = T{}, T{}
local floor, log10 = math.floor, math.log10
local function unprimable(n)
if sieve[n] then return false end
local nd = floor(log10(n))+1
for i = 1, nd do
local pow10 = 10^(nd-i)
for j = 0, 9 do
local p = (floor(n/10/pow10) * 10 + j) * pow10 + (n % pow10)
if sieve[p] then return false end
end
end
return true
end
local n, done = 1, 0
while done < 10 do
if unprimable(n) then
unprimables:insert(n)
if not lowests[n%10] then
lowests[n%10] = n
done = done + 1
end
end
n = n + 1
end
-- OUTPUT:
local function commafy(i) return tostring(i):reverse():gsub("(%d%d%d)","%1,"):reverse():gsub("^,","") end
print("The first 35 unprimable numbers are:")
print(unprimables:firstn(35):concat(" "))
print()
print("The 600th unprimable number is: " .. commafy(unprimables[600]))
print()
print("The lowest unprimable number that ends in..")
for i = 0, 9 do
print(" " .. i .. " is: " .. commafy(lowests[i]))
end
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Bracmat programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the OCaml programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Prolog programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Scala programming language