Skip to content

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
merged 18 commits into from
Jun 1, 2025

Conversation

triuyen
Copy link
Contributor

@triuyen triuyen commented May 23, 2025

Description

This PR adds an implementation of Euler's totient function φ(n) to the number theory section.

  • Calculates φ(n) = count of integers ≤ n that are coprime to n (share no common factors except 1)
  • Uses prime factorization method for efficient computation
  • Complements the existing compute_totient function by providing single-value calculation

Type of change

  • Input: Single number n (vs. computing all values up to n)
  • Output: Single φ(n) value (vs. vector of all values)
  • Use case: Optimized for calculating φ(n) for one specific number, especially large numbers
  • Algorithm: Prime factorization approach with O(√n) complexity

Implementation details

  • Function signature: pub fn eulers_totient(n: u64) -> u64
  • Handles edge cases (n = 1)
  • Uses integer arithmetic throughout (no floating point)
  • Efficient prime factorization with early termination

Checklist:

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

triuyen added 2 commits May 23, 2025 11:50
…rime factorization method

- Includes comprehensive tests for small numbers, primes, prime powers, and larger values
- All tests pass and follows project naming conventions
@triuyen triuyen requested review from imp2002 and vil02 as code owners May 23, 2025 10:12
@codecov-commenter
Copy link

codecov-commenter commented May 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.47%. Comparing base (ba3f671) to head (ed43486).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #882      +/-   ##
==========================================
- Coverage   95.51%   95.47%   -0.04%     
==========================================
  Files         317      318       +1     
  Lines       22954    22975      +21     
==========================================
+ Hits        21924    21935      +11     
- Misses       1030     1040      +10     

☔ 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.

@vil02
Copy link
Member

vil02 commented May 23, 2025

I am having a hard time with this one, cf. #875 (review).

@siriak: what is your opinion?

@triuyen: in case it will be decided that it will be merged: please reach the 100% coverage and express the tests in a more parametrized way.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

The code looks good, I think we can still include this one because it's more efficient, it doesn't allocate all divisors in array to get their number. Just make sure to parameterize tests, thanks!

@triuyen
Copy link
Contributor Author

triuyen commented May 25, 2025

Thanks! I’ll go ahead and parameterize the tests as suggested. Let me know if anything else is needed before merging.

@triuyen triuyen closed this May 31, 2025
@triuyen triuyen reopened this May 31, 2025
@vil02 vil02 mentioned this pull request May 31, 2025
7 tasks
@vil02
Copy link
Member

vil02 commented May 31, 2025

@triuyen: regarding the failing clippy: #883 should resolve that issue. As soon as it will be merged, please update your branch.

@triuyen triuyen closed this May 31, 2025
@triuyen triuyen reopened this May 31, 2025
@triuyen triuyen closed this May 31, 2025
@triuyen triuyen reopened this May 31, 2025
@triuyen triuyen closed this May 31, 2025
@triuyen
Copy link
Contributor Author

triuyen commented May 31, 2025

@vil02 Okay Thanks for the info about #883 . I will update my branch after the merge

@triuyen triuyen reopened this Jun 1, 2025
@triuyen triuyen closed this Jun 1, 2025
@triuyen triuyen reopened this Jun 1, 2025
@vil02 vil02 merged commit a3b116d into TheAlgorithms:master Jun 1, 2025
4 checks passed
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.

4 participants