Extra primes: Difference between revisions

m (Forth - do a bit extra)
Line 1,233:
35: 7723
36: 7727</pre>
 
=={{header|Lua}}==
{{trans|C}}
<lang lua>function next_prime_digit_number(n)
if n == 0 then
return 2
end
local r = n % 10
if r == 2 then
return n + 1
end
if r == 3 or r == 5 then
return n + 2
end
return 2 + next_prime_digit_number(math.floor(n / 10)) * 10
end
 
function is_prime(n)
if n < 2 then
return false
end
 
if n % 2 == 0 then
return n == 2
end
if n % 3 == 0 then
return n == 3
end
if n % 5 == 0 then
return n == 5
end
 
local wheel = { 4, 2, 4, 2, 4, 6, 2, 6 }
local p = 7
while true do
for w = 1, #wheel do
if p * p > n then
return true
end
if n % p == 0 then
return false
end
p = p + wheel[w]
end
end
end
 
function digit_sum(n)
local sum = 0
while n > 0 do
sum = sum + n % 10
n = math.floor(n / 10)
end
return sum
end
 
local limit1 = 10000
local limit2 = 1000000000
local last = 10
local p = 0
local n = 0
local extra_primes = {}
 
print("Extra primes under " .. limit1 .. ":")
while true do
p = next_prime_digit_number(p)
if p >= limit2 then
break
end
if is_prime(digit_sum(p)) and is_prime(p) then
n = n + 1
if p < limit1 then
print(string.format("%2d: %d", n, p))
end
extra_primes[n % last] = p
end
end
 
print(string.format("\nLast %d extra primes under %d:", last, limit2))
local i = last - 1
while i >= 0 do
print(string.format("%d: %d", n - i, extra_primes[(n - i) % last]))
i = i - 1
end</lang>
{{out}}
<pre>Extra primes under 10000:
1: 2
2: 3
3: 5
4: 7
5: 23
6: 223
7: 227
8: 337
9: 353
10: 373
11: 557
12: 577
13: 733
14: 757
15: 773
16: 2333
17: 2357
18: 2377
19: 2557
20: 2753
21: 2777
22: 3253
23: 3257
24: 3323
25: 3527
26: 3727
27: 5233
28: 5237
29: 5273
32: 7237
33: 7253
34: 7523
35: 7723
36: 7727
 
Last 10 extra primes under 1000000000:
9049: 777753773
9050: 777755753
9051: 777773333
9052: 777773753
9053: 777775373
9054: 777775553
9055: 777775577
9056: 777777227
9057: 777777577
9058: 777777773</pre>
 
=={{header|MAD}}==
1,452

edits