How to resolve the algorithm Primality by Wilson's theorem step by step in the ALGOL W 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 ALGOL W 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 ALGOL W programming language
Source code in the algol programming language
begin
% find primes using Wilson's theorem: %
% p is prime if ( ( p - 1 )! + 1 ) mod p = 0 %
% returns true if n is a prime by Wilson's theorem, false otherwise %
% computes the factorial mod p at each stage, so as to %
% allow numbers whose factorial won't fit in 32 bits %
logical procedure isWilsonPrime ( integer value n ) ;
begin
integer factorialModN;
factorialModN := 1;
for i := 2 until n - 1 do factorialModN := ( factorialModN * i ) rem n;
factorialModN = n - 1
end isWilsonPrime ;
for i := 1 until 100 do if isWilsonPrime( i ) then writeon( i_w := 1, s_w := 0, " ", i );
end.
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Dinesman's multiple-dwelling problem step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/For step by step in the LC3 Assembly programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Tcl programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the REXX programming language