Skip to content

Commit 76daf2d

Browse files
committed
Add collatz-conjecture
1 parent 2642de3 commit 76daf2d

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
module CollatzConjecture (collatz) where
22

3-
collatz_internal :: Integer -> Integer
4-
collatz_internal num
5-
| num == 1 = 0
6-
| isEven = 1 + collatz_internal (num `div` 2)
7-
| otherwise = 1 + collatz_internal (3 * num + 1)
8-
where
9-
isEven = rem num 2 == 0
10-
113
collatz :: Integer -> Maybe Integer
124
collatz num
135
| num > 0 = Just (collatz_internal num)
146
| otherwise = Nothing
7+
where
8+
collatz_internal pnum
9+
| pnum == 1 = 0
10+
| even pnum = 1 + collatz_internal (pnum `div` 2)
11+
| otherwise = 1 + collatz_internal (3 * pnum + 1)
1512

0 commit comments

Comments
 (0)