1
0
Fork 0

Re-arrange, remove cruft and random primes

main
Bill Ewanick 2023-10-20 02:31:03 -04:00
parent 78d4ca5aea
commit 08a37af729
1 changed files with 16 additions and 24 deletions

40
src/3n+1/Main.hs Executable file → Normal file
View File

@ -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 $ take 100 $ map f [2^1000..]
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]