How to resolve the algorithm Primality by Wilson's theorem step by step in the Raku 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 Raku 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 Raku programming language
Source code in the raku programming language
sub postfix: (Int $n) { (constant f = 1, |[\*] 1..*)[$n] }
sub is-wilson-prime (Int $p where * > 1) { (($p - 1)! + 1) %% $p }
# Pre initialize factorial routine (not thread safe)
9000!;
# Testing
put ' p prime?';
printf("%4d %s\n", $_, .&is-wilson-prime) for 2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659;
my $wilsons = (2,3,*+2…*).hyper.grep: &is-wilson-prime;
put "\nFirst 120 primes:";
put $wilsons[^120].rotor(20)».fmt('%3d').join: "\n";
put "\n1000th through 1015th primes:";
put $wilsons[999..1014];
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Go programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Dc programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Arrays step by step in the Brainf*** programming language