elm/exercises/collatz-conjecture/CollatzConjecture.example.elm

20 lines
451 B
Elm
Raw Normal View History

2017-10-07 16:36:25 +00:00
module CollatzConjecture exposing (collatz)
collatz : Int -> Result String Int
collatz start =
if start <= 0 then
Err "Only positive numbers are allowed"
else
Ok (collatzHelper 0 start)
2017-10-08 09:30:04 +00:00
collatzHelper : Int -> Int -> Int
2017-10-07 16:36:25 +00:00
collatzHelper steps start =
if start == 1 then
steps
else if start % 2 == 0 then
collatzHelper (1 + steps) (start // 2)
else
collatzHelper (1 + steps) (3 * start + 1)