Re-arrange, remove cruft and random primes
parent
78d4ca5aea
commit
358aee8d56
|
@ -6,6 +6,22 @@ https://youtu.be/094y1Z2wpJg
|
||||||
import Control.Monad ()
|
import Control.Monad ()
|
||||||
import Debug.Trace (trace)
|
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' :: Integer -> Integer
|
||||||
f' n = f'' 1 n
|
f' n = f'' 1 n
|
||||||
where
|
where
|
||||||
|
@ -22,25 +38,8 @@ f' n = f'' 1 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"
|
t'' = trace "\n#######################################################\n"
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
main :: IO ()
|
|
||||||
main = print $ map f [2^361..]
|
|
||||||
|
|
||||||
-- collatz collect
|
-- collatz collect
|
||||||
-- generate the collatz sequence and return it
|
-- generate the collatz sequence and return it
|
||||||
|
|
||||||
cc :: Integer -> [Integer]
|
cc :: Integer -> [Integer]
|
||||||
cc n = cc' [] n
|
cc n = cc' [] n
|
||||||
where
|
where
|
||||||
|
@ -54,10 +53,3 @@ cc n = cc' [] n
|
||||||
| 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]
|
||||||
|
|
||||||
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