How to resolve the algorithm Primality by Wilson's theorem step by step in the Ring 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 Ring 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 Ring programming language

Source code in the ring programming language

load "stdlib.ring"

decimals(0)
limit = 19

for n = 2 to limit
    fact = factorial(n-1) + 1
    see "Is " + n + " prime: "
    if fact % n = 0
       see "1" + nl
    else
       see "0" + nl
    ok
next

# primality by Wilson's theorem

limit = 100

for n = 1 to limit
    if isWilsonPrime( n )
       see " " + n
    ok
next n

func isWilsonPrime n
    fmodp = 1
    for i = 2 to n - 1
        fmodp *= i
        fmodp %= n
    next i
    return fmodp = n - 1

  

You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Honaker primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Woma programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Lang5 programming language