How to resolve the algorithm Primality by Wilson's theorem step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

## Compute (n - 1)! mod m.
def facmod($n; $m):
  reduce range(2; $n+1) as $k (1; (. * $k) % $m);
 
def isPrime: .>1 and (facmod(. - 1; .) + 1) % . == 0;

"Prime numbers between 2 and 100:",
[range(2;101) | select (isPrime)],

# Notice that `infinite` can be used as the second argument of `range`:
"First 10 primes after 7900:",
[limit(10; range(7900; infinite) | select(isPrime))]

Prime numbers between 2 and 100:
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
First 10 primes after 7900:
[7901,7907,7919,7927,7933,7937,7949,7951,7963,7993]


  

You may also check:How to resolve the algorithm Solve a Hidato puzzle step by step in the D programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the Tcl programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Arturo programming language
You may also check:How to resolve the algorithm Tau number step by step in the Action! programming language
You may also check:How to resolve the algorithm Honaker primes step by step in the F# programming language