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

import           Control.Applicative
import           Control.Monad
import           Data.List
import           Text.Printf

--y^2= x^3+ax+b mod n
import System.Random
data Elliptic = Conelliptic Integer Integer Integer deriving (Eq,Show)
data Point = Conpoint Integer Integer | Identity deriving (Eq,Show)

--add points of elliptic curve
addPoints::Elliptic->Point->Point-> Either Point Integer
addPoints _ Identity p_2 = Left p_2
addPoints _ p_1 Identity = Left p_1
addPoints (Conelliptic a b n) (Conpoint x_p y_p) (Conpoint x_q y_q)
    | x_p/=x_q = let
            [u,v,d]= extended_gcd (x_p-x_q) n
            s=mod ((y_p-y_q)*u) n
            x_r=mod (s*s-x_p-x_q) n
            y_r=mod (-y_p-s*(x_r-x_p)) n
             in case ((Conpoint x_r y_r),d) of
              (_,1)->Left (Conpoint x_r y_r)
              (_,d')->Right d'
    | otherwise = if mod (y_p+y_q) n ==0 then Left Identity
             else  let
                  [u,v,d]= extended_gcd (2*y_p) n
                  s=mod ((3*x_p*x_p+a)*u) n
                  x_r=mod (s*s-2*x_p) n
                  y_r=mod (-y_p-s*(x_r-x_p)) n
               in case ((Conpoint x_r y_r),d) of
                                (_,1)->Left (Conpoint x_r y_r)
                                (_,d')->Right d'



extended_gcd::Integer->Integer->[Integer]
extended_gcd a b=
    let
       [u,v,d]=exgcd a b
        where
        exgcd a b | mod a b == 0 = [0,1,b]
                  | otherwise = [y,x-y*(div a b),z]
                  where
                            [x,y,z] = exgcd b (mod a b)
    in if d<0 then [-u,-v,-d] else [u,v,d]



multiEC::Elliptic->Point->Integer->Either Point Integer
multiEC _ _ 0 = Left Identity
multiEC ecurve point k | k>0 = fun Identity point k
    where
        fun p _ 0 = Left p
        fun p q n = let
                p'=if odd n then addPoints ecurve p q else Left p
                q'= addPoints ecurve q q
                in case (p',q') of
                    (Left p'',Left q'')-> fun p'' q'' (div n 2)
                    (Right p'',_)-> Right p''
                    (_,Right q'')->Right q''




dscrmntEC a b = 4 * a * a * a + 27 * b * b

randomCurve ::Integer->  IO [Integer]
randomCurve n= do
        a<-getStdRandom (randomR (1,n))
        u<-getStdRandom (randomR (1,n))
        v<-getStdRandom (randomR (1,n))
        let b=mod (v*v-u*u*u-a*u) n
        return [a,b,n,u,v]


factor::Integer->Integer->Integer->IO [Integer]
factor 1 _ _ = return []
factor n m cnt | cnt>=5 = return [n]
           | otherwise = do
                [a,b,n,u,v]<-randomCurve n
                let p=multiEC (Conelliptic a b n) (Conpoint u v ) m
                case p of
                    Left p' -> factor n m (cnt+1)
                    Right p'-> do
                            ps<-factor (div n p') m 0 --start again factoring
                            ls<-factor p' m 0 --start again factoring
                            return (ls++ps)

main = do
        line<-getLine
        let n=read line ::Integer
        m<-factor n (foldl lcm 1 [1..100]) 0
        -- print m
        let prime_factors = nub m
            num = length prime_factors in
                print $ product (map (\p-> n - n `div` p) prime_factors) `div` (n ^ fromIntegral (num - 1))
        return ()