Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
* [Haversine](https://github.com/TheAlgorithms/Rust/blob/master/src/navigation/haversine.rs)
* Number Theory
* [Compute Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/compute_totient.rs)
* [Euler Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/euler_totient.rs)
* [Kth Factor](https://github.com/TheAlgorithms/Rust/blob/master/src/number_theory/kth_factor.rs)
* Searching
* [Binary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/binary_search.rs)
Expand Down
70 changes: 70 additions & 0 deletions src/number_theory/euler_totient.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pub fn euler_totient(n: u64) -> u64 {
if n == 1 {
return 1;
}

Comment thread
triuyen marked this conversation as resolved.
Outdated
let mut result = n;
let mut num = n;
let mut p = 2;

// Find all prime factors and apply formula
while p * p <= num {
// Check if p is a divisor of n
if num % p == 0 {
// If yes, then it is a prime factor
// Apply the formula: result = result * (1 - 1/p)
while num % p == 0 {
num /= p;
}
result -= result / p;
}
p += 1;
}

// If num > 1, then it is a prime factor
if num > 1 {
result -= result / num;
}

result
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_small_numbers() {
assert_eq!(euler_totient(1), 1);
assert_eq!(euler_totient(2), 1);
assert_eq!(euler_totient(3), 2);
assert_eq!(euler_totient(4), 2);
assert_eq!(euler_totient(5), 4);
assert_eq!(euler_totient(6), 2);
}

#[test]
fn test_prime_numbers() {
// For prime p, φ(p) = p - 1
assert_eq!(euler_totient(7), 6);
assert_eq!(euler_totient(11), 10);
assert_eq!(euler_totient(13), 12);
assert_eq!(euler_totient(17), 16);
}

#[test]
fn test_prime_powers() {
// For prime power p^k, φ(p^k) = p^(k-1) * (p-1)
assert_eq!(euler_totient(9), 6); // 3^2, φ(9) = 3^1 * 2 = 6
assert_eq!(euler_totient(25), 20); // 5^2, φ(25) = 5^1 * 4 = 20
assert_eq!(euler_totient(8), 4); // 2^3, φ(8) = 2^2 * 1 = 4
}

#[test]
fn test_larger_numbers() {
assert_eq!(euler_totient(10), 4);
assert_eq!(euler_totient(12), 4);
assert_eq!(euler_totient(100), 40);
assert_eq!(euler_totient(1000), 400);
}
}
2 changes: 2 additions & 0 deletions src/number_theory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod compute_totient;
mod euler_totient;
mod kth_factor;

pub use self::compute_totient::compute_totient;
pub use self::euler_totient::euler_totient;
pub use self::kth_factor::kth_factor;