Find prime n such that reversed n is also prime: Difference between revisions

Added Lua
(Added Algol 68)
(Added Lua)
Line 869:
{{out}}
<pre>{2,3,5,7,11,13,17,31,37,71,73,79,97,101,107,113,131,149,151,157,167,179,181,191,199,311,313,337,347,353,359,373,383,389}</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find primes whose reversed digits are also prime
local function isPrime( p )
if p <= 1 or p % 2 == 0 then
return p == 2
else
local prime = true
local i = 3
local rootP = math.floor( math.sqrt( p ) )
while i <= rootP and prime do
prime = p % i ~= 0
i = i + 2
end
return prime
end
end
local function reverseDigits( n )
return tonumber( string.reverse( tostring( n ) ) )
end
local count = 0
for n = 1,500 do
if isPrime( n ) then
if isPrime( reverseDigits( n ) ) then
count = count + 1
io.write( string.format( "%3d", n ), count % 10 == 0 and "\n" or " " )
end
end
end
io.write( "\nFound ", count, " reversable primes up to 500" )
end
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157
167 179 181 191 199 311 313 337 347 353
359 373 383 389
Found 34 reversable primes up to 500
</pre>
 
=={{header|MAD}}==
3,021

edits