From 956e56028f6ca1b19fcb4b988c64d77d986bd6e6 Mon Sep 17 00:00:00 2001 From: Bill Ewanick Date: Fri, 20 Oct 2023 02:14:34 -0400 Subject: [PATCH] Add small collatz details --- src/3n+1/Main.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/3n+1/Main.hs b/src/3n+1/Main.hs index dd08d89..37f7d9f 100755 --- a/src/3n+1/Main.hs +++ b/src/3n+1/Main.hs @@ -1,3 +1,8 @@ +{- +The Simplest Math Problem No One Can Solve - Collatz Conjecture +https://youtu.be/094y1Z2wpJg +-} + import Control.Monad () import Debug.Trace (trace) @@ -7,10 +12,10 @@ f' n = f'' 0 n f'' :: Integer -> Integer -> Integer f'' i n' | n' == 1 = t' i - | even n' = t $ f'' (i + 1) (n' `div` 2) - | odd n' = t $ f'' (i + 1) (3*n' + 1) + | 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') - t' = t'' $ trace ("END: " ++ show n ++ ", length: " <> show i) $ t'' + t' = t'' $ trace ("END: " ++ show n ++ ", length: " <> show i) t'' t'' = trace "\n#######################################################\n" f :: Integer -> Integer @@ -21,7 +26,7 @@ f n = s 0 n | n == 1 = i | even n = s i' (n `div` 2) | odd n = s i' (3*n + 1) - where i' = i + 1 + where i' = succ i main :: IO () main = print $ map f [2^361..]