Mersenne primes: Difference between revisions

Added Lua version
(Added Lua version)
Line 604:
2 ^ 4423 - 1
</pre>
 
=={{header|Lua}}==
This checks for primality using a trial division function. The limitation is 'until p == p + 1', meaning that the program will halt when Lua's number type (a 64-bit floating point value) no longer has enough precision to distiguish between one integer and the next.
<lang lua>-- Returns boolean indicate whether x is prime
function isPrime (x)
if x < 2 then return false end
if x < 4 then return true end
if x % 2 == 0 then return false end
for d = 3, math.sqrt(x), 2 do
if x % d == 0 then return false end
end
return true
end
 
-- Main procedure
local i, p = 0
repeat
i = i + 1
p = 2 ^ i - 1
if isPrime(p) then
print("2 ^ " .. i .. " - 1 = " .. p)
end
until p == p + 1</lang>
{{out}}
<pre>2 ^ 2 - 1 = 3
2 ^ 3 - 1 = 7
2 ^ 5 - 1 = 31
2 ^ 7 - 1 = 127
2 ^ 13 - 1 = 8191
2 ^ 17 - 1 = 131071
2 ^ 19 - 1 = 524287
2 ^ 31 - 1 = 2147483647</pre>
 
=={{header|PARI/GP}}==
Anonymous user