Unprimeable numbers: Difference between revisions

Line 771:
 
=={{header|Lua}}==
<lang lua>-- 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</lang>
{{out}}
<pre>The first 35 unprimable numbers are:
200 204 206 208 320 322 324 325 326 328 510 512 514 515 516 518 530 532 534 535 536 538 620 622 624 625 626 628 840 842 844 845 846 848 890
 
The 600th unprimable number is: 5,242
 
The lowest unprimable number that ends in..
0 is: 200
1 is: 595,631
2 is: 322
3 is: 1,203,623
4 is: 204
5 is: 325
6 is: 206
7 is: 872,897
8 is: 208
9 is: 212,159</pre>
 
=={{header|Pascal}}==
Anonymous user