Euler totient function problem

In summary, the problem is that the naive approach to calculating totient phi(n) using a range of consecutive integers will lead to Too Many Errors (TLE). However, there are more advanced algorithms that can be used to factor the input integers up to 10^14.
  • #1
Saitama
4,243
93
Following is the problem I am trying to solve: SPOJ.com - Problem ETFS

In number theory, the totient phi(n) of a positive integer n is defined to be the number of positive integers less than or equal to n that are coprime to n.

Input

The lonely line in input contains two integers a, b.

Output

Print phi(n) for n from a to b (inclusive).

Example

Input:
1 5

Output:
1
1
2
2
4

Constraints

0 < a < b < 10^14
b - a < 10^5

Here is my code:

Code:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

typedef long long ll;

ll solve(ll n)
{
	ll i, ans=n;
	for(i=2; i*i<=n; ++i)
	{
		if(n%i==0)
			ans-=ans/i;
		while(n%i==0)
			n/=i;
	}
	if(n>1)
		ans-=ans/n;
	return ans;
}

int main() {
	ll T, n, a, b, i;
	scanf("%lld %lld", &a, &b);
	vector<ll> x(b-a+1, -1);
	for(n=a; n<=b; ++n)
	{
		if(x[n-a]==-1)
		{
			x[n-a]=solve(n);
			if(2*n<=b)
			{
				if(n%2) x[2*n-a]=x[n-a];
				else x[2*n-a]=2*x[n-a];
			}
			for(i=n*4; i<=b; i*=2)
			{
				x[i-a]=2*x[i/2-a];
			}
		}
	}
	for(i=0; i<b-a+1; ++i)
		printf("%lld\n", x[i]);
	return 0;
}

Some explanation of what I am trying to do.

The naive approach i.e finding $\phi (n)$ of every number in the given range using the $O(\sqrt{n})$ won't work. So whenever I have found $\phi(n)$, I use the following relation: $\phi(2n)=2\phi(n)$ if $n$ is even and $\phi(2n)=\phi(n)$ is $n$ is odd. With this, I don't have to run the solve() function ($O(\sqrt{n}$) for every integer in the range but even with this optimisation, I get TLE. :(

Please help. Thanks!
 
Technology news on Phys.org
  • #2
Hi Pranav,

Perhaps we can use that
\begin{cases}\phi(p)&=p-1\\\phi(p^n)&=(p-1)p^{n-1}\\\phi(a\cdot p^n)&=\phi(a)\cdot\phi(p^n) &\text{ if }p\nmid a\end{cases}
Then we might apply a sieve or dynamic programming.
 
  • #3
Hint: trial division is not the only factorization algorithm that exists. There are many more advanced algorithms, some of which are easy to implement and will pretty much instantly factor your input integers up to 10^14. For instance, Pollard's Rho is pretty simple to implement, and in practice runs in worst case $O(\sqrt[4]{n})$.

Then you can use I Like Serena's suggestions to take advantage of the fact that you have to factor a range of consecutive integers, etc...
 

1. What is the Euler totient function?

The Euler totient function, also known as Euler's phi function, is a mathematical function that counts the number of positive integers less than or equal to a given number n that are relatively prime to n. It is denoted by the symbol φ(n).

2. How is the Euler totient function calculated?

The Euler totient function is calculated by multiplying n by the product of (1 - 1/p) for each prime factor p of n. For example, if n = 10, the prime factors are 2 and 5, so the Euler totient function would be calculated as φ(10) = 10 x (1 - 1/2) x (1 - 1/5) = 4.

3. What is the significance of the Euler totient function?

The Euler totient function has many important applications in number theory, cryptography, and other areas of mathematics. It is used to solve problems related to modular arithmetic, primality testing, and finding solutions to certain equations. It also plays a key role in the RSA encryption algorithm.

4. Can the Euler totient function be used to find the prime factorization of a number?

No, the Euler totient function alone cannot be used to find the prime factorization of a number. However, it can be used in conjunction with other methods, such as the Chinese remainder theorem, to find the prime factorization of a number.

5. Is there a simple way to calculate the Euler totient function for large numbers?

Unfortunately, there is no simple formula for calculating the Euler totient function for large numbers. However, there are efficient algorithms that can be used to calculate it, such as the Euler's product formula and the sieve of Eratosthenes. These methods can be implemented in computer programs to compute the Euler totient function for very large numbers.

Similar threads

  • Programming and Computer Science
Replies
1
Views
944
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
311
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top