Published by Dimitris Kougioumtzis · Updated
Remove each distinct prime factor from the count using φ(n) = n ∏(1−1/p).
Remove the fraction divisible by each distinct prime factor to count residues coprime to n.
When to use Euler totient
Counting reduced residue classes.
Choosing an alternative
Factorization tracks multiplicity, whereas the totient adjustment uses each prime once.
Worked example: input and state changes
Example input
36
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ 36, 36 ], "active": [] }Step 1: Apply distinct prime factor 2; state: totient, remaining factorization.
State at this checkpoint
{ "values": [ 18, 9 ], "active": [ 0, 1 ] }Step 2: Apply distinct prime factor 3; state: totient, remaining factorization.
State at this checkpoint
{ "values": [ 12, 1 ], "active": [ 0, 1 ] }Step 3: Euler totient = 12.
State at this checkpoint
{ "values": [ 12, 1 ], "active": [ 0 ] }
Expected final state
Euler totient = 12.
Complexity and input bounds
Family: Number theory. Time complexity: O(√n).
Space complexity: O(1) trial-division state.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
One integer from 1 to 9999.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Repeated powers of one prime must not trigger repeated totient adjustments.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 | // n from 1 to 9999. Counts integers 1..n coprime to n, including φ(1)=1. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(n) { let result = n; for (let p = 2; p * p <= n; p++) { if (n % p == 0) { result -= Math.floor(result / p); while (n % p == 0) { n = Math.floor(n / p); } } } if (n > 1) { result -= Math.floor(result / n); } return result; } |
Source
No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.
- The Art of Computer Programming, Volume 2: Seminumerical Algorithms (3rd edition)
Donald E. Knuth (1997). Reference book.