From b639d14933a7189a63e2b6b9a94ebe6db8f2b273 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 1 Jun 2025 15:28:33 +0200 Subject: [PATCH 1/4] style: include `elidable_lifetime_names` (#887) --- Cargo.toml | 1 - src/data_structures/binary_search_tree.rs | 2 +- src/data_structures/veb_tree.rs | 2 +- src/math/random.rs | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e911f831a3f..5ac4b4e1f77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,6 @@ unused_self = { level = "allow", priority = 1 } used_underscore_binding = { level = "allow", priority = 1 } ref_option = { level = "allow", priority = 1 } unnecessary_semicolon = { level = "allow", priority = 1 } -elidable_lifetime_names = { level = "allow", priority = 1 } # restriction-lints: absolute_paths = { level = "allow", priority = 1 } arithmetic_side_effects = { level = "allow", priority = 1 } diff --git a/src/data_structures/binary_search_tree.rs b/src/data_structures/binary_search_tree.rs index e5767c0ed4b..193fb485408 100644 --- a/src/data_structures/binary_search_tree.rs +++ b/src/data_structures/binary_search_tree.rs @@ -184,7 +184,7 @@ where stack: Vec<&'a BinarySearchTree>, } -impl<'a, T> BinarySearchTreeIter<'a, T> +impl BinarySearchTreeIter<'_, T> where T: Ord, { diff --git a/src/data_structures/veb_tree.rs b/src/data_structures/veb_tree.rs index b928be080f4..fe5fd7fc06d 100644 --- a/src/data_structures/veb_tree.rs +++ b/src/data_structures/veb_tree.rs @@ -221,7 +221,7 @@ impl<'a> VebTreeIter<'a> { } } -impl<'a> Iterator for VebTreeIter<'a> { +impl Iterator for VebTreeIter<'_> { type Item = u32; fn next(&mut self) -> Option { diff --git a/src/math/random.rs b/src/math/random.rs index 88e87866b06..de218035484 100644 --- a/src/math/random.rs +++ b/src/math/random.rs @@ -107,7 +107,7 @@ impl PCG32 { } } -impl<'a> Iterator for IterMut<'a> { +impl Iterator for IterMut<'_> { type Item = u32; fn next(&mut self) -> Option { Some(self.pcg.get_u32()) From a9ed3ed2075a0593fc258bdaa5bed59e18cc74f5 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 1 Jun 2025 15:32:30 +0200 Subject: [PATCH 2/4] style: include `enum_glob_use` (#888) --- Cargo.toml | 1 - src/sorting/dutch_national_flag_sort.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ac4b4e1f77..e592c3da29e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,6 @@ cast_precision_loss = { level = "allow", priority = 1 } cast_sign_loss = { level = "allow", priority = 1 } cloned_instead_of_copied = { level = "allow", priority = 1 } doc_markdown = { level = "allow", priority = 1 } -enum_glob_use = { level = "allow", priority = 1 } explicit_deref_methods = { level = "allow", priority = 1 } explicit_iter_loop = { level = "allow", priority = 1 } float_cmp = { level = "allow", priority = 1 } diff --git a/src/sorting/dutch_national_flag_sort.rs b/src/sorting/dutch_national_flag_sort.rs index 14a5ac72166..7d24d6d0321 100644 --- a/src/sorting/dutch_national_flag_sort.rs +++ b/src/sorting/dutch_national_flag_sort.rs @@ -11,7 +11,7 @@ pub enum Colors { White, // | Define the three colors of the Dutch Flag: 🇳🇱 Blue, // / } -use Colors::*; +use Colors::{Blue, Red, White}; // Algorithm implementation pub fn dutch_national_flag_sort(mut sequence: Vec) -> Vec { From ba3f67102811e390c5c580432b69b64629976a50 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sun, 1 Jun 2025 15:33:05 +0200 Subject: [PATCH 3/4] style: include `needless_lifetimes` (#889) --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e592c3da29e..ad1e70c5d45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -165,6 +165,5 @@ doc_lazy_continuation = { level = "allow", priority = 1 } needless_return = { level = "allow", priority = 1 } doc_overindented_list_items = { level = "allow", priority = 1 } # complexity-lints -needless_lifetimes = { level = "allow", priority = 1 } precedence = { level = "allow", priority = 1 } manual_div_ceil = { level = "allow", priority = 1 } From a3b116d3203acd4e26982735f49c3b9c7743cb61 Mon Sep 17 00:00:00 2001 From: Ttang <52043791+triuyen@users.noreply.github.com> Date: Sun, 1 Jun 2025 17:55:24 +0200 Subject: [PATCH 4/4] Add euler totient function (#882) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 <65706193+vil02@users.noreply.github.com> * Update src/number_theory/euler_totient.rs Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * add all remainning cases * add suggestion and add other cases * Update euler_totient.rs Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * before merge * re-test --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- DIRECTORY.md | 1 + src/number_theory/euler_totient.rs | 74 ++++++++++++++++++++++++++++++ src/number_theory/mod.rs | 2 + 3 files changed, 77 insertions(+) create mode 100644 src/number_theory/euler_totient.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index b09bebb48e5..d7eff5fe286 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/src/number_theory/euler_totient.rs b/src/number_theory/euler_totient.rs new file mode 100644 index 00000000000..69c0694a335 --- /dev/null +++ b/src/number_theory/euler_totient.rs @@ -0,0 +1,74 @@ +pub fn euler_totient(n: u64) -> u64 { + 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::*; + macro_rules! test_euler_totient { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (input, expected) = $test_case; + assert_eq!(euler_totient(input), expected) + } + )* + }; + } + + test_euler_totient! { + prime_2: (2, 1), + prime_3: (3, 2), + prime_5: (5, 4), + prime_7: (7, 6), + prime_11: (11, 10), + prime_13: (13, 12), + prime_17: (17, 16), + prime_19: (19, 18), + + composite_6: (6, 2), // 2 * 3 + composite_10: (10, 4), // 2 * 5 + composite_15: (15, 8), // 3 * 5 + composite_12: (12, 4), // 2^2 * 3 + composite_18: (18, 6), // 2 * 3^2 + composite_20: (20, 8), // 2^2 * 5 + composite_30: (30, 8), // 2 * 3 * 5 + + prime_power_2_to_2: (4, 2), + prime_power_2_to_3: (8, 4), + prime_power_3_to_2: (9, 6), + prime_power_2_to_4: (16, 8), + prime_power_5_to_2: (25, 20), + prime_power_3_to_3: (27, 18), + prime_power_2_to_5: (32, 16), + + // Large numbers + large_50: (50, 20), // 2 * 5^2 + large_100: (100, 40), // 2^2 * 5^2 + large_1000: (1000, 400), // 2^3 * 5^3 + } +} diff --git a/src/number_theory/mod.rs b/src/number_theory/mod.rs index 7d2e0ef14f6..0500ad775d1 100644 --- a/src/number_theory/mod.rs +++ b/src/number_theory/mod.rs @@ -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;