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 Scala (judge verdict: Accepted)

import scala.io.StdIn.{readLine, readInt}

object Main {

  def factorize(p:Long,d:Long,l:Set[Long]):Set[Long] = {
    if(d >= Math.sqrt(p)+1) {
      if(p != 1) l+p else l
    } else {
      if(p%d == 0)
        factorize(p/d,d,l+d)
      else if(d==2)
        factorize(p,d+1,l)
      else
        factorize(p,d+2,l)
    }
  }

  def main(args:Array[String]) = {
    val n = readInt
    val l = factorize(n,2,Set())
    var pi = n.toLong
    for(i <- l) {
      pi = (pi*(i-1))/i
    }
    println(pi)
  }
}