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

Source code in the action! programming language

;;; returns TRUE(1) if p is prime by Wilson's theorem, FALSE(0) otherwise
;;;         computes the factorial mod p at each stage, so as to allow
;;;         for numbers whose factorial won't fit in 16 bits
BYTE FUNC isWilsonPrime( CARD p )
  CARD i, factorial_mod_p
  BYTE result

  factorial_mod_p = 1
  FOR i = 2 TO p - 1 DO
    factorial_mod_p = ( factorial_mod_p * i ) MOD p
  OD

  IF factorial_mod_p = p - 1 THEN result = 1 ELSE result = 0 FI

RETURN( result )

PROC Main()
  CARD i

  FOR i = 1 TO 100 DO
    IF isWilsonPrime( i ) THEN
       Put(' ) PrintC( i )
    FI
  OD
RETURN

  

You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the jq programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Racket programming language
You may also check:How to resolve the algorithm Meissel–Mertens constant step by step in the Python programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Java programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the PicoLisp programming language