Compare commits
No commits in common. "358aee8d5641b918e6845802a4703d0795450d52" and "baf8ad2559f1b2e4fbe0e0970351927ae74020f5" have entirely different histories.
358aee8d56
...
baf8ad2559
|
@ -1,55 +1,47 @@
|
|||
{-
|
||||
The Simplest Math Problem No One Can Solve - Collatz Conjecture
|
||||
https://youtu.be/094y1Z2wpJg
|
||||
-}
|
||||
|
||||
import Control.Monad ()
|
||||
import Debug.Trace (trace)
|
||||
|
||||
main :: IO ()
|
||||
main = print $ map f [2^100000..]
|
||||
|
||||
f :: Integer -> Integer
|
||||
f n = s 1 n
|
||||
where
|
||||
s :: Integer -> Integer -> Integer
|
||||
s i n
|
||||
| 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)
|
||||
|
||||
f' :: Integer -> Integer
|
||||
f' n = f'' 1 n
|
||||
f' n = f'' 0 n
|
||||
where
|
||||
f'' :: Integer -> Integer -> Integer
|
||||
f'' i n'
|
||||
| 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)
|
||||
| n' == 1 = t' i
|
||||
| even n' = t $ f'' (i + 1) (n' `div` 2)
|
||||
| odd n' = t $ f'' (i + 1) (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
|
||||
f n = s 0 n
|
||||
where
|
||||
s :: Integer -> Integer -> Integer
|
||||
s i n
|
||||
| n == 1 = i
|
||||
| even n = s i' (n `div` 2)
|
||||
| odd n = s i' (3*n + 1)
|
||||
where i' = i + 1
|
||||
|
||||
main :: IO ()
|
||||
main = print $ map f [2^361..]
|
||||
|
||||
-- collatz collect
|
||||
-- generate the collatz sequence and return it
|
||||
|
||||
cc :: Integer -> [Integer]
|
||||
cc n = cc' [] n
|
||||
where
|
||||
cc' :: [Integer] -> Integer -> [Integer]
|
||||
cc' acc n
|
||||
| n == 1 = acc <> [1]
|
||||
| n == 0 = acc <> [0]
|
||||
| n == (-1) = acc <> [-1]
|
||||
| n == (-5) = acc <> [-5]
|
||||
| n == (-17) = acc <> [-17]
|
||||
| n == 1 = 1:acc
|
||||
| even n = cc' acc' (n `div` 2)
|
||||
| odd n = cc' acc' (3*n + 1)
|
||||
where acc' = acc <> [n]
|
||||
|
||||
primes :: [Integer]
|
||||
primes = sieve [2..]
|
||||
where
|
||||
sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0]
|
||||
|
||||
isPrime k = (k > 1) && null [ x | x <- [2..k - 1], k `mod` x == 0]
|
||||
|
|
Loading…
Reference in New Issue