1
0
Fork 0

Account for negative loops

main
Bill Ewanick 2023-10-20 02:23:50 -04:00
parent a3dda37643
commit 78d4ca5aea
1 changed files with 15 additions and 3 deletions

View File

@ -11,7 +11,11 @@ f' n = f'' 1 n
where where
f'' :: Integer -> Integer -> Integer f'' :: Integer -> Integer -> Integer
f'' i n' f'' i n'
| n' == 1 = t' i | n' == 1 = t' 0 + t i
| n' == 0 = t' 0 + t i
| n' == (-1 ) = t' 0 + t i
| n' == (-5 ) = t' 0 + t i
| n' == (-17) = t' 0 + t i
| even n' = t $ f'' (succ i) (n' `div` 2) | even n' = t $ f'' (succ i) (n' `div` 2)
| odd n' = t $ f'' (succ i) (3*n' + 1) | odd n' = t $ f'' (succ i) (3*n' + 1)
where t = trace ("i: " ++ show i ++ ", number: " ++ show n') where t = trace ("i: " ++ show i ++ ", number: " ++ show n')
@ -24,6 +28,10 @@ f n = s 1 n
s :: Integer -> Integer -> Integer s :: Integer -> Integer -> Integer
s i n s i n
| n == 1 = i | n == 1 = i
| n == 0 = i
| n == (-1) = i
| n == (-5) = i
| n == (-17) = i
| even n = s (succ i) (n `div` 2) | even n = s (succ i) (n `div` 2)
| odd n = s (succ i) (3*n + 1) | odd n = s (succ i) (3*n + 1)
@ -38,7 +46,11 @@ cc n = cc' [] n
where where
cc' :: [Integer] -> Integer -> [Integer] cc' :: [Integer] -> Integer -> [Integer]
cc' acc n cc' acc n
| n == 1 = 1:acc | n == 1 = acc <> [1]
| n == 0 = acc <> [0]
| n == (-1) = acc <> [-1]
| n == (-5) = acc <> [-5]
| n == (-17) = acc <> [-17]
| even n = cc' acc' (n `div` 2) | even n = cc' acc' (n `div` 2)
| odd n = cc' acc' (3*n + 1) | odd n = cc' acc' (3*n + 1)
where acc' = acc <> [n] where acc' = acc <> [n]