From 78d4ca5aeaf1bfc52eb4c4999755a9e0bf1d4a22 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Fri, 20 Oct 2023 02:23:50 -0400 Subject: [PATCH] Account for negative loops --- src/3n+1/Main.hs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/3n+1/Main.hs b/src/3n+1/Main.hs index e42bbb5..b516e74 100755 --- a/src/3n+1/Main.hs +++ b/src/3n+1/Main.hs @@ -11,7 +11,11 @@ f' n = f'' 1 n where f'' :: Integer -> Integer -> Integer 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) | odd n' = t $ f'' (succ i) (3*n' + 1) where t = trace ("i: " ++ show i ++ ", number: " ++ show n') @@ -23,7 +27,11 @@ f n = s 1 n where s :: Integer -> Integer -> Integer 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) | odd n = s (succ i) (3*n + 1) @@ -38,7 +46,11 @@ cc n = cc' [] n where cc' :: [Integer] -> Integer -> [Integer] 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) | odd n = cc' acc' (3*n + 1) where acc' = acc <> [n]