-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add euler totient function #882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vil02
merged 18 commits into
TheAlgorithms:master
from
triuyen:add-euler-totient-function
Jun 1, 2025
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f80b6cd
Add Euler's totient function implementation - Implements φ(n) using p…
triuyen dcef568
add to DIRECTORY.md
triuyen ee2b107
Add parameterized tests for Euler totient with 100% coverage
triuyen 2c67875
Add parameterized tests for Euler totient with 100% coverage
triuyen ebcb859
Add parameterized tests for Euler totient with 100% coverage
triuyen 02ab7fe
Add parameterized tests for Euler totient with 100% coverage / after …
triuyen f44ddb5
code syntaxe fixing
triuyen c9221ed
run cargo clippy and cargo fmt
triuyen 4f80fef
re-test and make sure branch is up to date
triuyen 5b4453e
Update src/number_theory/euler_totient.rs
triuyen 51651bf
Update src/number_theory/euler_totient.rs
triuyen 4ca98bc
add all remainning cases
triuyen f4cc976
add suggestion and add other cases
triuyen ad48849
Update euler_totient.rs
triuyen 1ee2df3
before merge
triuyen aa82472
Merge branch 'add-euler-totient-function' of https://github.com/triuy…
triuyen 6e1ce73
re-test
triuyen ed43486
Merge branch 'master' into add-euler-totient-function
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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), | ||
|
||
// Small | ||
small_4: (4, 2), | ||
small_6: (6, 2), | ||
|
||
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_3: (8, 4), | ||
prime_power_3_to_2: (9, 6), | ||
prime_power_2_to_4: (16, 8), // 2^4 | ||
prime_power_5_to_2: (25, 20), // 5^2 | ||
prime_power_3_to_3: (27, 18), // 3^3 | ||
prime_power_2_to_5: (32, 16), // 2^5 | ||
|
||
// 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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.