How to resolve the algorithm Primality by Wilson's theorem step by step in the Wren 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 Wren 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 Wren programming language
Source code in the wren programming language
import "/math" for Int
import "/fmt" for Fmt
var wilson = Fn.new { |p|
if (p < 2) return false
return (Int.factorial(p-1) + 1) % p == 0
}
for (p in 1..19) {
Fmt.print("$2d -> $s", p, wilson.call(p) ? "prime" : "not prime")
}
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm UPC step by step in the Python programming language
You may also check:How to resolve the algorithm 24 game step by step in the Perl programming language
You may also check:How to resolve the algorithm System time step by step in the JavaScript programming language