How to resolve the algorithm Primality by Wilson's theorem step by step in the Rust 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 Rust 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 Rust programming language
Source code in the rust programming language
fn factorial_mod(mut n: u32, p: u32) -> u32 {
let mut f = 1;
while n != 0 && f != 0 {
f = (f * n) % p;
n -= 1;
}
f
}
fn is_prime(p: u32) -> bool {
p > 1 && factorial_mod(p - 1, p) == p - 1
}
fn main() {
println!(" n | prime?\n------------");
for p in vec![2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659] {
println!("{:>3} | {}", p, is_prime(p));
}
println!("\nFirst 120 primes by Wilson's theorem:");
let mut n = 0;
let mut p = 1;
while n < 120 {
if is_prime(p) {
n += 1;
print!("{:>3}{}", p, if n % 20 == 0 { '\n' } else { ' ' });
}
p += 1;
}
println!("\n1000th through 1015th primes:");
let mut i = 0;
while n < 1015 {
if is_prime(p) {
n += 1;
if n >= 1000 {
i += 1;
print!("{:>3}{}", p, if i % 16 == 0 { '\n' } else { ' ' });
}
}
p += 1;
}
}
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Forth programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the AWK programming language
You may also check:How to resolve the algorithm Send email step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Python programming language