Repository navigation
Conversation
The sieve broke on i * primes[j] >= n, so index n was never updated and totient(n)[n] stayed n - 1 for every composite n (totient(10)[10] == 9, phi(10) == 4). Use > n so the last entry is sieved too.
mindaugl
approved these changes
Nov 17, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change
Problem
maths/eulers_totient.pytotient(n)is a linear sieve that returns a list of lengthn + 1withtotients[i] = φ(i). The sieve stops one index too early:Because of
>=, indexnis never marked as composite or updated, sototients[n]keeps its initial valuen - 1(the formula for a prime). For every compositen, the last entry of the returned list is wrong. The doctest prints only indices1..n-1, so CI never sees it.Reproduced on free-threaded CPython 3.15.2 (the version in
.python-version) at30507bb:For
n = 2..300,totient(n)[n]is wrong for 237 of 299 values: every compositen.Fix
i * primes[j] <= nis always a valid index into then + 1-long lists, so the bound can includen.This is a code-only change. The existing doctest prints indices 1..9 for
n = 10, and those are the same before and after.Behaviour change, checked against master
For every
nin0..400, I compared the full returned lists on master and on this branch:0..n-1: identical (0 differences)n: changed for 321 values ofn, all of them compositenthat were wrong on masteri >= 2equals a brute-forceφ(i) = #{k ≤ i : gcd(k, i) = 1}Index 1 is still
0, as the current doctest asserts (φ(1) is 1 by definition). I left that alone because changing it would change the doctest output.Validation (free-threaded CPython 3.15.2, macOS arm64)
totient(10)[10] == 4,totient(n)[n] == φ(n)forn = 2..300, andtotient(200)[2:]against brute force) fails on master and passes with this change. I kept it out of this PR because AGENTS.md asks not to change code and its doctests in the same PR. I can open the doctests as a follow-up PR if that helps.uv run --with=pytest-run-parallel pytest --iterations=8 --parallel-threads=auto --ignore-gil-enabled maths/eulers_totient.py: all pass.pytestcommand frombuild.ymlgives the same result as on master (4139 passed, plus the same 2 environment-dependent failures as master on macOS: thegaussian_distributionRNG doctest and the network-dependentinstagram_crawlerdoctest).uvx pre-commit run --files maths/eulers_totient.py: all hooks pass.uvx ruff check .: all checks passed.Checklist
🤖 Generated with Claude Code