RosettaCodeData/Task/Jacobi-symbol/Pluto/jacobi-symbol.pluto
2026-04-30 12:34:36 -04:00

29 lines
817 B
Text

local fmt = require "fmt"
local function jacobi(a, n)
if n % 1 != 0 or n <= 0 or n % 2 == 0 then
error("The 'n' parameter must be an odd positive integer.")
end
a %= n
local result = 1
while a != 0 do
while a % 2 == 0 do
a /= 2
local nm8 = n % 8
if nm8 == 3 or nm8 == 5 then result = -result end
end
a, n = n, a
if a % 4 == 3 and n % 4 == 3 then result = -result end
a %= n
end
return n == 1 ? result : 0
end
print("Table of jacobi(a, n):")
print("n/a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
print("---------------------------------------------------------------")
for n = 1, 29, 2 do
fmt.write("%3d", n)
for a = 1, 15 do fmt.write("%4d", jacobi(a, n)) end
print()
end