Skip to content

Commit 2c5be4a

Browse files
author
allansomensi
committed
Add Euler's Totient function implementation
1 parent b4aecf4 commit 2c5be4a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
* [Doomsday](https://github.com/TheAlgorithms/Rust/blob/master/src/math/doomsday.rs)
195195
* [Elliptic Curve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/elliptic_curve.rs)
196196
* [Euclidean Distance](https://github.com/TheAlgorithms/Rust/blob/master/src/math/euclidean_distance.rs)
197+
* [Euler Totient](https://github.com/TheAlgorithms/Rust/blob/master/src/math/euler_totient.rs)
197198
* [Exponential Linear Unit](https://github.com/TheAlgorithms/Rust/blob/master/src/math/exponential_linear_unit.rs)
198199
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/math/extended_euclidean_algorithm.rs)
199200
* [Factorial](https://github.com/TheAlgorithms/Rust/blob/master/src/math/factorial.rs)

src/math/euler_totient.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// ## Euler's Totient (`φ(n)`)
2+
///
3+
/// Calculate the **Euler's Totient** function of a given number `n`.
4+
///
5+
/// Wikipedia: https://en.wikipedia.org/wiki/Euler%27s_totient_function
6+
pub fn euler_totient(mut n: i64) -> i64 {
7+
let mut result = n;
8+
9+
if n <= 0 {
10+
panic!("Must be a positive integer!");
11+
}
12+
13+
for i in 2..=n.isqrt() {
14+
if n % i == 0 {
15+
while n % i == 0 {
16+
n /= i;
17+
}
18+
result -= result / i;
19+
}
20+
}
21+
22+
if n > 1 {
23+
result -= result / n;
24+
}
25+
26+
result
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn test_euler_totient() {
35+
assert_eq!(euler_totient(1), 1);
36+
assert_eq!(euler_totient(325), 240);
37+
assert_eq!(euler_totient(746), 372);
38+
assert_eq!(euler_totient(3_639), 2_424);
39+
assert_eq!(euler_totient(98_354), 49_176);
40+
assert_eq!(euler_totient(123_456), 41_088);
41+
assert_eq!(euler_totient(493_123_235), 347_518_080);
42+
assert_eq!(euler_totient(945_243_784_032), 315_074_904_192);
43+
assert_eq!(euler_totient(372_036_854_775_808), 185_661_377_740_800);
44+
}
45+
}

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod decimal_to_fraction;
1919
mod doomsday;
2020
mod elliptic_curve;
2121
mod euclidean_distance;
22+
mod euler_totient;
2223
mod exponential_linear_unit;
2324
mod extended_euclidean_algorithm;
2425
pub mod factorial;
@@ -103,6 +104,7 @@ pub use self::decimal_to_fraction::decimal_to_fraction;
103104
pub use self::doomsday::get_week_day;
104105
pub use self::elliptic_curve::EllipticCurve;
105106
pub use self::euclidean_distance::euclidean_distance;
107+
pub use self::euler_totient::euler_totient;
106108
pub use self::exponential_linear_unit::exponential_linear_unit;
107109
pub use self::extended_euclidean_algorithm::extended_euclidean_algorithm;
108110
pub use self::factorial::{factorial, factorial_bigmath, factorial_recursive};

0 commit comments

Comments
 (0)