Skip to content

Add Euler's Totient function implementation #875

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

Closed
wants to merge 1 commit into from
Closed

Add Euler's Totient function implementation #875

wants to merge 1 commit into from

Conversation

allansomensi
Copy link

Adds the implementation of the Euler's Totient function with a unit test.

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@codecov-commenter
Copy link

codecov-commenter commented Mar 11, 2025

Codecov Report

Attention: Patch coverage is 96.55172% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.49%. Comparing base (b4aecf4) to head (2c5be4a).

Files with missing lines Patch % Lines
src/math/euler_totient.rs 96.55% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #875   +/-   ##
=======================================
  Coverage   95.49%   95.49%           
=======================================
  Files         316      317    +1     
  Lines       22919    22948   +29     
=======================================
+ Hits        21887    21915   +28     
- Misses       1032     1033    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality is (almost) provided by compute_totient.rs. Both implementations are similarly efficient. @siriak what do you suggest?

/// Calculate the **Euler's Totient** function of a given number `n`.
///
/// Uncyclopedia: https://en.wikipedia.org/wiki/Euler%27s_totient_function
pub fn euler_totient(mut n: i64) -> i64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about having the following signature:

Suggested change
pub fn euler_totient(mut n: i64) -> i64 {
pub fn euler_totient(n: u64) -> Result<u64, &'static str> {

Comment on lines +35 to +43
assert_eq!(euler_totient(1), 1);
assert_eq!(euler_totient(325), 240);
assert_eq!(euler_totient(746), 372);
assert_eq!(euler_totient(3_639), 2_424);
assert_eq!(euler_totient(98_354), 49_176);
assert_eq!(euler_totient(123_456), 41_088);
assert_eq!(euler_totient(493_123_235), 347_518_080);
assert_eq!(euler_totient(945_243_784_032), 315_074_904_192);
assert_eq!(euler_totient(372_036_854_775_808), 185_661_377_740_800);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about having more parametrized approach like in test_aliquot_sum.

@allansomensi
Copy link
Author

Oops! I hadn’t noticed this algorithm was already here. Thanks for pointing it out! Closing the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants