How to resolve the algorithm Totient function step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Totient function step by step in the Rust programming language
Table of Contents
Problem Statement
The totient function is also known as:
The totient function:
If the totient number (for N) is one less than N, then N is prime.
Create a totient function and: Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Totient function step by step in the Rust programming language
Source code in the rust programming language
use num::integer::gcd;
fn main() {
// Compute the totient of the first 25 natural integers
println!("N\t phi(n)\t Prime");
for n in 1..26 {
let phi_n = phi(n);
println!("{}\t {}\t {:?}", n, phi_n, phi_n == n - 1);
}
// Compute the number of prime numbers for various steps
[1, 100, 1000, 10000, 100000]
.windows(2)
.scan(0, |acc, tuple| {
*acc += (tuple[0]..=tuple[1]).filter(is_prime).count();
Some((tuple[1], *acc))
})
.for_each(|x| println!("Until {}: {} prime numbers", x.0, x.1));
}
fn is_prime(n: &usize) -> bool {
phi(*n) == *n - 1
}
fn phi(n: usize) -> usize {
(1..=n).filter(|&x| gcd(n, x) == 1).count()
}
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the PureBasic programming language