loading...

re: Write a script to find "Happy Numbers" VIEW POST

FULL DISCUSSION
 

F#

let rec extractDigits acc input = 
    if input <= 9 then input::acc  
    else extractDigits (input % 10::acc) (input/10)

let sumOfSquares input = 
    input
    |> List.fold(fun acc i -> acc + i*i) 0

let rec isHappy input =
    if input = 1 then true
    else if input >= 2 && input <=6 then false
    else 
        let newInput = 
            input
            |> extractDigits List.empty
            |> sumOfSquares
        isHappy newInput

[<EntryPoint>]
let main argv =
    [1..10000]
    |> List.filter(fun i -> isHappy i)
    |> List.iter(fun i -> printfn "%d" i)        
    0
Code of Conduct Report abuse