Problem 34 - Euler's totient

totient :  Int -> Int
totient n = 
    List.length <| List.filter (\x -> coprime n x) (List.range 1 n)


coprime :  Int -> Int -> Bool
coprime a b = 
    gcd a b == 1


gcd : Int -> Int -> Int 
gcd a b =
    if b == 0 then  
        abs a
    else
        gcd b (a % b)

Back to problem

results matching ""

    No results matching ""