How to resolve the algorithm Primality by Wilson's theorem step by step in the Sidef 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 Sidef 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 Sidef programming language
Source code in the sidef programming language
func is_wilson_prime_slow(n) {
n > 1 || return false
(n-1)! % n == n-1
}
func is_wilson_prime_fast(n) {
n > 1 || return false
factorialmod(n-1, n) == n-1
}
say 25.by(is_wilson_prime_slow) #=> [2, 3, 5, ..., 83, 89, 97]
say 25.by(is_wilson_prime_fast) #=> [2, 3, 5, ..., 83, 89, 97]
say is_wilson_prime_fast(2**43 - 1) #=> false
say is_wilson_prime_fast(2**61 - 1) #=> true
You may also check:How to resolve the algorithm Array concatenation step by step in the Apex programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the Wren programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the RPL programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the EchoLisp programming language