Factorial primes: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Simplified also make it more DRY and less WET) |
(Added Lua) |
||
Line 912: | Line 912: | ||
9: 12! - 1 = 479001599 |
9: 12! - 1 = 479001599 |
||
10: 14! - 1 = 87178291199 |
10: 14! - 1 = 87178291199 |
||
</pre> |
|||
=={{header|Lua}}== |
|||
{{Trans|ALGOL 68}} |
|||
<syntaxhighlight lang="lua"> |
|||
do -- find some factorial primes - primes that are f - 1 or f + 1 |
|||
-- for some factorial f |
|||
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 + 1 |
|||
end |
|||
return prime |
|||
end |
|||
end |
|||
local f = 1 |
|||
local fpCount = 0 |
|||
local n = 0 |
|||
local fpOp = "" |
|||
while fpCount < 10 do |
|||
n = n + 1 |
|||
f = f * n |
|||
fpOp = "-" |
|||
for fp = f - 1, f + 1, 2 do |
|||
if isPrime( fp ) then |
|||
fpCount = fpCount + 1 |
|||
io.write( string.format( "%2d", fpCount ), ":" |
|||
, string.format( "%4d", n ), "! " |
|||
, fpOp, " 1 = ", fp, "\n" |
|||
) |
|||
end |
|||
fpOp = "+" |
|||
end |
|||
end |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1: 1! + 1 = 2 |
|||
2: 2! + 1 = 3 |
|||
3: 3! - 1 = 5 |
|||
4: 3! + 1 = 7 |
|||
5: 4! - 1 = 23 |
|||
6: 6! - 1 = 719 |
|||
7: 7! - 1 = 5039 |
|||
8: 11! + 1 = 39916801 |
|||
9: 12! - 1 = 479001599 |
|||
10: 14! - 1 = 87178291199 |
|||
</pre> |
</pre> |
||