Sunday, 6 January 2013

Euler Totient Function


SPOJ Problem Set (classical)

4141. Euler Totient Function

Problem code: ETF

English Vietnamese
In number theory, the totient  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.

Given an integer n (1 <= n <= 10^6). Compute the value of the totient .

Input

First line contains an integer T, the number of test cases. (T <= 20000)

T following lines, each contains an integer n.

Output

T lines, one for the result of each test case.

Example

Input:
5
1
2
3
4
5

Output:
1
1
2
2
4



EXPLAINATION--
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=primeNumbers

(TOPCODER FORUM)

SOLUTION--

#include
int phi(int n)
{
     int result = n;
     int i;
       for(i=2;i*i <= n;i++) 
       { 
         if (n % i == 0) 
         result -= result / i; 
         while (n % i == 0) 
         n /= i; 
       } 
       if (n > 1)
       result -= result / n; 
       return result; 
}
int main()
{
    int t,num;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&num);
        printf("%d\n",phi(num));
    }
    return 0;
}

No comments: