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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace NTL1D
{
    class Program
    {
        static void Main(string[] args)
        {
            var prime = new Prime(100000);
            Console.WriteLine(prime.EulersTotient(int.Parse(Console.ReadLine())));
        }
    }

    public class Prime
    {
        readonly List<int> primes;
        public List<int> List { get { return primes.ToList(); } }

        public Prime(int max)
        {
            primes = new List<int>();

            var sieve = new bool[max + 1];
            for (var i = 2; i <= max; i++)
            {
                if (sieve[i]) continue;
                primes.Add(i);
                for (var j = i * 2; j <= max; j += i) sieve[j] = true;
            }
        }

        public bool Is(int n)
        {
            return n > 1 && primes.TakeWhile(p => p * p <= n).All(p => n % p != 0);
        }

        public IEnumerable<int> Factorize(int n)
        {
            foreach (var p in primes.TakeWhile(p => p * p <= n))
            {
                while (n % p == 0)
                {
                    n /= p;
                    yield return p;
                }
            }
            if (n != 1) yield return n;
        }

        public int EulersTotient(int n)
        {
            return Factorize(n).Distinct().Aggregate(n, (ret, x) => ret / x * (x - 1));
        }
    }
}