-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathcompute_totient.rs
60 lines (49 loc) · 1.24 KB
/
compute_totient.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Totient function for
// all numbers smaller than
// or equal to n.
// Computes and prints
// totient of all numbers
// smaller than or equal to n
use std::vec;
pub fn compute_totient(n: i32) -> vec::Vec<i32> {
let mut phi: Vec<i32> = Vec::new();
// initialize phi[i] = i
for i in 0..=n {
phi.push(i);
}
// Compute other Phi values
for p in 2..=n {
// If phi[p] is not computed already,
// then number p is prime
if phi[(p) as usize] == p {
// Phi of a prime number p is
// always equal to p-1.
phi[(p) as usize] = p - 1;
// Update phi values of all
// multiples of p
for i in ((2 * p)..=n).step_by(p as usize) {
phi[(i) as usize] = (phi[i as usize] / p) * (p - 1);
}
}
}
phi[1..].to_vec()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
assert_eq!(
compute_totient(12),
vec![1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4]
);
}
#[test]
fn test_2() {
assert_eq!(compute_totient(7), vec![1, 1, 2, 2, 4, 2, 6]);
}
#[test]
fn test_3() {
assert_eq!(compute_totient(4), vec![1, 1, 2, 2]);
}
}