Skip to content

Commit a3b116d

Browse files
triuyenvil02
andauthored
Add euler totient function (#882)
* Add Euler's totient function implementation - Implements φ(n) using prime factorization method - Includes comprehensive tests for small numbers, primes, prime powers, and larger values - All tests pass and follows project naming conventions * add to DIRECTORY.md * Add parameterized tests for Euler totient with 100% coverage * Add parameterized tests for Euler totient with 100% coverage * Add parameterized tests for Euler totient with 100% coverage * Add parameterized tests for Euler totient with 100% coverage / after echo * code syntaxe fixing * run cargo clippy and cargo fmt * re-test and make sure branch is up to date * Update src/number_theory/euler_totient.rs Co-authored-by: Piotr Idzik <[email protected]> * Update src/number_theory/euler_totient.rs Co-authored-by: Piotr Idzik <[email protected]> * add all remainning cases * add suggestion and add other cases * Update euler_totient.rs Co-authored-by: Piotr Idzik <[email protected]> * before merge * re-test --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent ba3f671 commit a3b116d

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
* [Haversine](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/haversine.rs)
263263
* Number Theory
264264
* [Compute Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/compute_totient.rs)
265+
* [Euler Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/euler_totient.rs)
265266
* [Kth Factor](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs)
266267
* Searching
267268
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)

src/number_theory/euler_totient.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
pub fn euler_totient(n: u64) -> u64 {
2+
let mut result = n;
3+
let mut num = n;
4+
let mut p = 2;
5+
6+
// Find all prime factors and apply formula
7+
while p * p <= num {
8+
// Check if p is a divisor of n
9+
if num % p == 0 {
10+
// If yes, then it is a prime factor
11+
// Apply the formula: result = result * (1 - 1/p)
12+
while num % p == 0 {
13+
num /= p;
14+
}
15+
result -= result / p;
16+
}
17+
p += 1;
18+
}
19+
20+
// If num > 1, then it is a prime factor
21+
if num > 1 {
22+
result -= result / num;
23+
}
24+
25+
result
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
macro_rules! test_euler_totient {
32+
($($name:ident: $test_case:expr,)*) => {
33+
$(
34+
#[test]
35+
fn $name() {
36+
let (input, expected) = $test_case;
37+
assert_eq!(euler_totient(input), expected)
38+
}
39+
)*
40+
};
41+
}
42+
43+
test_euler_totient! {
44+
prime_2: (2, 1),
45+
prime_3: (3, 2),
46+
prime_5: (5, 4),
47+
prime_7: (7, 6),
48+
prime_11: (11, 10),
49+
prime_13: (13, 12),
50+
prime_17: (17, 16),
51+
prime_19: (19, 18),
52+
53+
composite_6: (6, 2), // 2 * 3
54+
composite_10: (10, 4), // 2 * 5
55+
composite_15: (15, 8), // 3 * 5
56+
composite_12: (12, 4), // 2^2 * 3
57+
composite_18: (18, 6), // 2 * 3^2
58+
composite_20: (20, 8), // 2^2 * 5
59+
composite_30: (30, 8), // 2 * 3 * 5
60+
61+
prime_power_2_to_2: (4, 2),
62+
prime_power_2_to_3: (8, 4),
63+
prime_power_3_to_2: (9, 6),
64+
prime_power_2_to_4: (16, 8),
65+
prime_power_5_to_2: (25, 20),
66+
prime_power_3_to_3: (27, 18),
67+
prime_power_2_to_5: (32, 16),
68+
69+
// Large numbers
70+
large_50: (50, 20), // 2 * 5^2
71+
large_100: (100, 40), // 2^2 * 5^2
72+
large_1000: (1000, 400), // 2^3 * 5^3
73+
}
74+
}

src/number_theory/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod compute_totient;
2+
mod euler_totient;
23
mod kth_factor;
34

45
pub use self::compute_totient::compute_totient;
6+
pub use self::euler_totient::euler_totient;
57
pub use self::kth_factor::kth_factor;

0 commit comments

Comments
 (0)