Add Collatz exploration
parent
8144edd676
commit
baf8ad2559
|
@ -0,0 +1,47 @@
|
||||||
|
import Control.Monad ()
|
||||||
|
import Debug.Trace (trace)
|
||||||
|
|
||||||
|
f' :: Integer -> Integer
|
||||||
|
f' n = f'' 0 n
|
||||||
|
where
|
||||||
|
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)
|
||||||
|
where t = trace ("i: " ++ show i ++ ", number: " ++ show n')
|
||||||
|
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 = 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