RosettaCodeData/Task/Primality-by-Wilsons-theorem/Pluto/primality-by-wilsons-theorem.pluto
2026-04-30 12:34:36 -04:00

24 lines
670 B
Text

local fmt = require "fmt"
local function wilson(p)
local facmod = 1
for i = 2, p - 1 do facmod = (facmod * i) % p end
return facmod + 1 == p
end
local primes = {2}
local i = 3
while #primes < 1015 do
if wilson(i) then primes:insert(i) end
i += 2
end
local candidates = {2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659}
print(" n | prime?\n------------")
for candidates as cand do fmt.print("%3d | %s", cand, wilson(cand)) end
print("\nThe first 120 prime numbers by Wilson's theorem are:")
fmt.tprint("%3d", primes:slice(1, 120), 20)
print("\nThe 1,000th to 1,015th prime numbers are:")
print(primes:slice(#primes - 15):concat(" "))