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

Source code in the fsharp programming language

// Wilsons theorem. Nigel Galloway: August 11th., 2020
let wP(n,g)=(n+1I)%g=0I
let fN=Seq.unfold(fun(n,g)->Some((n,g),((n*g),(g+1I))))(1I,2I)|>Seq.filter wP
fN|>Seq.take 120|>Seq.iter(fun(_,n)->printf "%A " n);printfn "\n"
fN|>Seq.skip 999|>Seq.take 15|>Seq.iter(fun(_,n)->printf "%A " n);printfn ""


  

You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the F# programming language
You may also check:How to resolve the algorithm Mayan numerals step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Mercury programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Factor programming language
You may also check:How to resolve the algorithm Boolean values step by step in the LFE programming language