1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Bill Ewanick baf8ad2559 Add Collatz exploration 2023-10-19 17:09:05 -04:00
Bill Ewanick 8144edd676 Thoughts on 1 2023-10-18 23:26:03 -04:00
Bill Ewanick bc3247a792 Flailing with 15 2023-10-18 23:25:50 -04:00
Bill Ewanick 9cc718e6b2 Question 16 2023-10-18 23:25:25 -04:00
4 changed files with 88 additions and 2 deletions

47
src/3n+1/Main.hs Executable file
View File

@ -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]

View File

@ -13,6 +13,9 @@ main = print ans
ans :: Integer
ans = sum $ allNumbersUnder 1000
sum' :: Num a => [a] -> a
sum' = foldl (+) 0
isMultipleOf :: Integral a => a -> a -> Bool
isMultipleOf n x = x `rem` n == 0

View File

@ -14,10 +14,31 @@ main = print ans
ans = 42
limit = 20
points = [(x,y) | x <- [1..limit], y <- [1..limit]]
limit = 2
points = [ (x,y)
| x <- [0..limit]
, y <- [0..limit]
]
data Direction = Right | Down
move Right (x,y) = (x+1, y )
move Down (x,y) = (x , y+1)
-- solve :: (Int, Int) -> [[(Int, Int)]]
{-
λ> [(0,0)]
[(0,0)]
λ> [(0,0),(1,0),(2,0),(2,1),(2,2)]
[(0,0),(1,0),(2,0),(2,1),(2,2)]
λ> [ [(0,0),(1,0),(2,0),(2,1),(2,2)], [(0,0),(1,0),(1,1),(2,1),(2,2)]]
[[(0,0),(1,0),(2,0),(2,1),(2,2)],[(0,0),(1,0),(1,1),(2,1),(2,2)]]
λ>
-}
data Tree a
= Leaf a
| Branch (Tree a) (Tree a)
deriving (Show)

View File

@ -0,0 +1,15 @@
{-
https://projecteuler.net/problem=16
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?
-}
module Main where
main :: IO ()
main = print ans
ans :: Integer
ans = sum $ map (\c -> read [c] :: Integer) $ show (2^1000)