Problem:
Euler's Phi Function

For given integer n, count the totatives of n, that is, the positive integers less than or equal to n that are relatively prime to n.

Input

n

An integer n (1 ≤ n ≤ 1000000000).

Output

The number of totatives in a line.

Sample Input 1

6

Sample Output 1

2

Sample Input 2

1000000

Sample Output 2

400000

Solution in C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
 
#define PRIME_MAX 1000000
int getprimes(int list[], int top){
    int i, j;
    int index = 0;
    char tmp[PRIME_MAX];
 
    for(i=0;i<top;i++) {
        tmp[i] = 1;
    }
    
    tmp[0] = tmp[1] = 0;
    for(i=2;i<top;i++) {
        if(tmp[i] == 1) {
            /* 素数 */
            list[index] = i;
            index ++;
            for(j=2;j*i<top;j++) {
                tmp[j*i] = 0;
            }
        }
    }
 
    list[index] = -1;
    return index;
}
 
int main(void) {
    int i, c, j;
    int n, base;
    int size, find;
    int primes[100000];
    int div[1000];
    int d, last_d;
    size = getprimes(primes, PRIME_MAX);
    
    while(scanf("%d\n", &base) == 1) {
        i = d = 0;
        n = base;
        last_d = 0;
        while(n > 1) {
            if((n % primes[i]) == 0) {
            	if(last_d != primes[i]) {
	                div[d] = primes[i];
	                d++;
            	}
                n /= primes[i];
                last_d = primes[i];
            } else {
                i++;
            }
             
            if(i >= size) {
                div[d] = n;
                d++;
                break;
            }
        }
    }
    
    c = 0;
    for(i=1;i<=base;i++) {
    	int find = 0;
        for(j=0;j<d;j++) {
        	if(i % div[j] == 0) {
        		find = 1;
        		break;
        	}
        }
        if(!find) c++;
    }
    printf("%d\n", c);

    return 0;
}