I wanted to optimize the sieve method for computing Euler's Totient (Phi) values from 1 to n. Basically, i came across this Quora comment :https://www.quora.com/What-is-the-fastest-function-to-calculate-phi-I-for-1-I-n (2nd solution that avoids division), but I am unable to wrap my head around the algorithm mentioned by the person. Any help appreciated.
2026-03-25 07:47:50.1774424870
Fast sieve for sum of Euler totient values (phi) values from 1 to 'n'
744 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail At
1
There are 1 best solutions below
Related Questions in NUMBER-THEORY
- Maximum number of guaranteed coins to get in a "30 coins in 3 boxes" puzzle
- Interesting number theoretical game
- Show that $(x,y,z)$ is a primitive Pythagorean triple then either $x$ or $y$ is divisible by $3$.
- About polynomial value being perfect power.
- Name of Theorem for Coloring of $\{1, \dots, n\}$
- Reciprocal-totient function, in term of the totient function?
- What is the smallest integer $N>2$, such that $x^5+y^5 = N$ has a rational solution?
- Integer from base 10 to base 2
- How do I show that any natural number of this expression is a natural linear combination?
- Counting the number of solutions of the congruence $x^k\equiv h$ (mod q)
Related Questions in ALGORITHMS
- Least Absolute Deviation (LAD) Line Fitting / Regression
- Do these special substring sets form a matroid?
- Modified conjugate gradient method to minimise quadratic functional restricted to positive solutions
- Correct way to prove Big O statement
- Product of sums of all subsets mod $k$?
- (logn)^(logn) = n^(log10+logn). WHY?
- Clarificaiton on barycentric coordinates
- Minimum number of moves to make all elements of the sequence zero.
- Translation of the work of Gauss where the fast Fourier transform algorithm first appeared
- sources about SVD complexity
Related Questions in TOTIENT-FUNCTION
- Reciprocal-totient function, in term of the totient function?
- A congruence with the Euler's totient function and sum of divisors function
- Identify sequences from OEIS or the literature, or find examples of odd integers $n\geq 1$ satisfying these equations related to odd perfect numbers
- For which natural numbers are $\phi(n)=2$?
- A congruence with the Euler's totient function and number of divisors function
- Does converge $\sum_{n=2}^\infty\frac{1}{\varphi(p_n-2)-1+p_n}$, where $\varphi(n)$ is the Euler's totient function and $p_n$ the $n$th prime number?
- On the behaviour of $\frac{1}{N}\sum_{k=1}^N\frac{\pi(\varphi(k)+N)}{\varphi(\pi(k)+N)}$ as $N\to\infty$
- On the solutions of an equation involving the Euler's totient function that is solved by the primes of Rassias' conjecture
- Are there any known methods for finding Upper/Lower bounds on the number of Totients of x less than another number y?
- How is Carmichael's function subgroup of Euler's Totient function?
Related Questions in SIEVE-THEORY
- Sieve of Eratosthenes : why $\sqrt n$ work?
- Bounding the number of semiprimes of the form $2p\pm1$
- Asymptotic relations of $x$ and $x/(\log\log x)$
- Modified sieve to find count all the divisors from 1 to n in o(n) time
- Finding Prime Factors of a number in $\log(n)$
- Probability that a random number avoids a set of prime divisors
- In the General Number Field Sieve, can we estimate the size of the matrix in terms of the number being factored?
- How to tell if a particular number will survive in this sieve?
- Generalization of Opperman's Conjecture
- How long would it take a computer to check that a number is not divisible by any 20-digit prime?
Trending Questions
- Induction on the number of equations
- How to convince a math teacher of this simple and obvious fact?
- Find $E[XY|Y+Z=1 ]$
- Refuting the Anti-Cantor Cranks
- What are imaginary numbers?
- Determine the adjoint of $\tilde Q(x)$ for $\tilde Q(x)u:=(Qu)(x)$ where $Q:U→L^2(Ω,ℝ^d$ is a Hilbert-Schmidt operator and $U$ is a Hilbert space
- Why does this innovative method of subtraction from a third grader always work?
- How do we know that the number $1$ is not equal to the number $-1$?
- What are the Implications of having VΩ as a model for a theory?
- Defining a Galois Field based on primitive element versus polynomial?
- Can't find the relationship between two columns of numbers. Please Help
- Is computer science a branch of mathematics?
- Is there a bijection of $\mathbb{R}^n$ with itself such that the forward map is connected but the inverse is not?
- Identification of a quadrilateral as a trapezoid, rectangle, or square
- Generator of inertia group in function field extension
Popular # Hahtags
second-order-logic
numerical-methods
puzzle
logic
probability
number-theory
winding-number
real-analysis
integration
calculus
complex-analysis
sequences-and-series
proof-writing
set-theory
functions
homotopy-theory
elementary-number-theory
ordinary-differential-equations
circles
derivatives
game-theory
definite-integrals
elementary-set-theory
limits
multivariable-calculus
geometry
algebraic-number-theory
proof-verification
partial-derivative
algebra-precalculus
Popular Questions
- What is the integral of 1/x?
- How many squares actually ARE in this picture? Is this a trick question with no right answer?
- Is a matrix multiplied with its transpose something special?
- What is the difference between independent and mutually exclusive events?
- Visually stunning math concepts which are easy to explain
- taylor series of $\ln(1+x)$?
- How to tell if a set of vectors spans a space?
- Calculus question taking derivative to find horizontal tangent line
- How to determine if a function is one-to-one?
- Determine if vectors are linearly independent
- What does it mean to have a determinant equal to zero?
- Is this Batman equation for real?
- How to find perpendicular vector to another vector?
- How to find mean and median from histogram
- How many sides does a circle have?
Let's do it for $n=10.$ Start with $m=2$ and compute $\phi(m^k)$ for as high as you need. We get $\phi(2)=(2-1)=1,$ $\phi(4)= 2(2-1) = 2$ and similarly, $\phi(8) = 4.$ Then we need to multiply the entry of every multiple of $8$ by $\phi(8)=4.$ That's just $8.$ Then, we multiply the entry of every multiple of $4$ that is not a multiple of $8$ by $\phi(4)=2.$ Then we multiply every multiple of $2$ that is not a multiple of $4$ by $\phi(2)=1.$ So we have $$ \begin{pmatrix}2 & 3 & 4 & 5 &6 &7 &8 &9 & 10\\ 1&1&2&1&1&1&4&1&1\end{pmatrix}.$$ (where we invisibly multiplied entries $2,$ $6,$ and $10$ by $1$.) Now we go to the next entry that is still a one... that's $m=3.$ We have $\phi(3)=2$ and $\phi(9)=6.$ So we multiply the $9$ entry by $6$ (since it's the only multiple of $9$ on the list). And we multiply $3$ and $6$ (the multiples of $3$ that aren't multiples of $9$) by $2,$ giving $$ \begin{pmatrix}2 & 3 & 4 & 5 &6 &7 &8 &9 & 10\\ 1&2&2&1&2&1&4&6&1\end{pmatrix}.$$ Now on to $m=5.$ We have $\phi(5)=4,$ and we multiply the $5$ and $10$ by $4,$ giving $$ \begin{pmatrix}2 & 3 & 4 & 5 &6 &7 &8 &9 & 10\\ 1&2&2&4&2&1&4&6&4\end{pmatrix}.$$ Finally for $m=7,$ we have $\phi(7)=6$ and so we get
$$ \begin{pmatrix}2 & 3 & 4 & 5 &6 &7 &8 &9 & 10\\ 1&2&2&4&2&6&4&6&4\end{pmatrix}.$$
This is my interpretation of what they're saying but I actually don't really like it cause the whole 'skipping the multiples of the higher power' thing seems like a headache to code up. Instead, I'd simply multiply all multiples of $2$ on the list by $1,$ then all the multiples of $4$ by $2,$ then all the multiples of $8$ by $2,$ then all the multiples of $16$ by $2,$ etc. Then I'd multiply all the multiples of $3$ by $2,$ then all the multiples of $9$ by $3,$ then all the multiples of $27$ by $3,$ etc. I think that's much more straightforward and also only uses multiplication.
(EDIT Actually, perhaps I've missed the point of the way they do it: their way does do fewer multiplications since they pre-multiply, say $\phi(27)$ together once, where as I do it once for every multiple of $27$ on the list.)
Here is a (hastily done) implementation of my way in python