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

Source code in the common programming language

(defun factorial (n)
  (if (< n 2) 1 (* n (factorial (1- n)))) )


(defun primep (n)
 "Primality test using Wilson's Theorem"
  (unless (zerop n)
    (zerop (mod (1+ (factorial (1- n))) n)) ))


  

You may also check:How to resolve the algorithm Vector products step by step in the XBS programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Odin programming language
You may also check:How to resolve the algorithm Minkowski question-mark function step by step in the Perl programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Commatizing numbers step by step in the Raku programming language