How to resolve the algorithm Primality by Wilson's theorem step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by Wilson's theorem step by step in the Lua programming language

Table of Contents

Problem Statement

Write a boolean function that tells whether a given integer is prime using Wilson's theorem. By Wilson's theorem, a number p is prime if and only if p divides (p - 1)! + 1. Remember that 1 and all non-positive integers are not prime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by Wilson's theorem step by step in the Lua programming language

Source code in the lua programming language

-- primality by Wilson's theorem
 
function isWilsonPrime( n )
    local fmodp = 1
    for i = 2, n - 1 do
        fmodp = fmodp * i
        fmodp = fmodp % n
    end
    return fmodp == n - 1
end
 
for n = -1, 100 do
    if isWilsonPrime( n ) then
       io.write( " " .. n )
    end
end


  

You may also check:How to resolve the algorithm Unicode variable names step by step in the Lua programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Sidef programming language
You may also check:How to resolve the algorithm MD5 step by step in the C++ programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the HolyC programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Racket programming language