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

Source code in the zkl programming language

var [const] BI=Import("zklBigNum");  // libGMP
fcn isWilsonPrime(p){
   if(p<=1 or (p%2==0 and p!=2)) return(False);
   BI(p-1).factorial().add(1).mod(p) == 0
}
fcn wPrimesW{ [2..].tweak(fcn(n){ isWilsonPrime(n) and n or Void.Skip }) }

numbers:=T(2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659);
println("  n  prime");
println("---  -----");
foreach n in (numbers){ println("%3d  %s".fmt(n, isWilsonPrime(n))) }

println("\nFirst 120 primes via Wilson's theorem:");
wPrimesW().walk(120).pump(Void, T(Void.Read,15,False), 
  fcn(ns){ vm.arglist.apply("%4d".fmt).concat(" ").println() });

println("\nThe 1,000th to 1,015th prime numbers are:");
wPrimesW().drop(999).walk(15).concat(" ").println();

  

You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Delphi programming language
You may also check:How to resolve the algorithm Active Directory/Search for a user step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the RPL programming language