File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ mod decimal_to_fraction;
1919mod doomsday;
2020mod elliptic_curve;
2121mod euclidean_distance;
22+ mod euler_totient;
2223mod exponential_linear_unit;
2324mod extended_euclidean_algorithm;
2425pub mod factorial;
@@ -103,6 +104,7 @@ pub use self::decimal_to_fraction::decimal_to_fraction;
103104pub use self :: doomsday:: get_week_day;
104105pub use self :: elliptic_curve:: EllipticCurve ;
105106pub use self :: euclidean_distance:: euclidean_distance;
107+ pub use self :: euler_totient:: euler_totient;
106108pub use self :: exponential_linear_unit:: exponential_linear_unit;
107109pub use self :: extended_euclidean_algorithm:: extended_euclidean_algorithm;
108110pub use self :: factorial:: { factorial, factorial_bigmath, factorial_recursive} ;
You can’t perform that action at this time.
0 commit comments