Skip to content

Commit 5d71489

Browse files
committed
Some more puzzles. As these get harder, I'm skipping way too many...
1 parent 051d9eb commit 5d71489

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

Diff for: Euler47.hs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Data.List
2+
3+
-- Sieve of Eratosthenes
4+
mult n k = (n `mod` k) == 0
5+
sqrt' :: Integral a => a -> Int
6+
sqrt' = floor . sqrt . fromIntegral
7+
isPrime 1 = False
8+
isPrime 2 = True
9+
isPrime n = all not $ map (mult n) $ takeWhile (<= sqrt' n) $ primes
10+
primes = filter isPrime [2..]
11+
12+
{-
13+
factors = factors' [] primes
14+
factors' acc _ 1 = acc
15+
factors' acc (p:ps) n
16+
| n `mod` p == 0 = factors' (p:acc) (p:ps) (n `div` p)
17+
| otherwise = factors' acc ps n
18+
-}
19+
20+
factors = map (factors' primes) [0..]
21+
factors' _ 0 = []
22+
factors' _ 1 = []
23+
factors' (p:ps) n
24+
| n `mod` p == 0 = p : factors !! (n `div` p)
25+
| otherwise = factors' ps n
26+
27+
factorCounts = map (length . nub) factors
28+
29+
k = 4
30+
numsWithKFactors = map fst . filter (\(_,f) -> f == k) . zip [0..] $ factorCounts
31+
32+
firstInteresting all@(n:ns)
33+
| take k all == [n..n-1+(fromIntegral k)] = n
34+
| otherwise = firstInteresting ns
35+
36+
main = print . firstInteresting $ numsWithKFactors
37+
--}

Diff for: Euler50.hs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Data.List
2+
3+
-- Sieve of Eratosthenes
4+
mult n k = (n `mod` k) == 0
5+
sqrt' :: Integral a => a -> Int
6+
sqrt' = floor . sqrt . fromIntegral
7+
isPrime 1 = False
8+
isPrime 2 = True
9+
isPrime n = all not $ map (mult n) $ takeWhile (<= sqrt' n) $ primes
10+
primes = filter isPrime [2..]
11+
12+
sumsOfPrimes = takeWhile (not . null) $ map (takeWhile (<1000000) . sumsOfPrimesWithLength) [1..]
13+
sumsOfPrimesWithLength k = map (\n -> sum . take k . drop n $ primes) [0..]
14+
15+
main = print . last . filter isPrime . concat $ sumsOfPrimes

Diff for: Euler52.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- I didn't write any code to solve this. I simply remembered an interesting fact about the decimal expansion of 1/7 and guessed.
2+
main = print 142857

Diff for: Euler56.hs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Data.List
2+
import Control.Monad
3+
4+
digits n
5+
| n < 10 = [n]
6+
| otherwise = digits (n `div` 10) ++ [n `mod` 10]
7+
8+
main = print . maximum . map (sum . digits) . liftM2 (^) [1..100] $ [1..100]

Diff for: Euler57.hs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Control.Monad
2+
import Data.List
3+
import Data.Maybe
4+
import Data.Ratio
5+
6+
next :: Rational -> Rational
7+
next n = 1 + 1/(1 + n)
8+
9+
expansions = iterate next 1
10+
11+
digitCount n = elemIndex True . map (n<) . map (10^) $ [0..]
12+
13+
longerNumerator rat = liftM2 (>) (digitCount p) $ digitCount q
14+
where p = numerator rat
15+
q = denominator rat
16+
17+
main = print . length . filter (fromJust . longerNumerator) . take 1000 $ expansions

Diff for: Euler58.hs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Data.List
2+
import Data.Ratio
3+
4+
diagDiffs = 2:2:2:2 : map (+2) diagDiffs
5+
diags = 1 : zipWith (+) diagDiffs diags
6+
7+
-- Sieve of Eratosthenes
8+
mult n k = (n `mod` k) == 0
9+
sqrt' :: Integral a => a -> Int
10+
sqrt' = floor . sqrt . fromIntegral
11+
isPrime 1 = False
12+
isPrime 2 = True
13+
isPrime n = all not $ map (mult n) $ takeWhile (<= sqrt' n) $ primes
14+
primes = filter isPrime [2..]
15+
16+
diagsPrime = map isPrime diags
17+
18+
-- n is half the side of the square (rounded down)
19+
primesAlongDiags n = length . filter id . take (4*n+1) $ diagsPrime
20+
totalDiagLength n = (4*n+1)
21+
22+
ratio n = primesAlongDiags n % totalDiagLength n
23+
24+
main = print . (\n -> 2*n+1) . head . filter (\n -> ratio n < 1%10) $ [1..]

Diff for: Euler61.hs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Data.List
2+
3+
poly n = 1 : zipWith (+) (poly n) [n-1, 2*n-3..]
4+
5+
elemIncreasing a (x:xs)
6+
| x == a = True
7+
| x > a = False
8+
| otherwise = elemIncreasing a xs
9+
10+
concatNum m n = m * 100 + n
11+
12+
{- Too slow
13+
sextuplets = [[a `concatNum` b, b `concatNum` c, c `concatNum` d, d `concatNum` e, e `concatNum` f, f `concatNum` a]
14+
| a <- [10..99], b <- [10..99], c <- [10..99], d <- [10..99], e <- [10..99], f <- [10..99]]
15+
16+
setsRepresented xs = [n | n <- [3..8], x <- xs, x `elemIncreasing` p n]
17+
-}
18+
19+
fourDigitOctagonals = takeWhile (<10000) . dropWhile (<1000) $ poly 8
20+
21+
-- h = upper two digits
22+
-- ps = list of figurates
23+
-- out = (figurates, answer)
24+
--findPentagonal :: Int -> [Int] -> [(Int, Int)]
25+
findPentagonal h ps = concat . map find' $ ps
26+
where find' p = zip (repeat p) . takeWhile (<(h+1)*100) . dropWhile (<1000) . dropWhile (<h*100) $ poly p
27+
28+
29+
first = map (\n -> ([3..7], [n])) fourDigitOctagonals
30+
31+
addOnePentagonal foo = foo >>= next
32+
next :: ([Integer], [Integer]) -> [([Integer], [Integer])]
33+
next (ps, ans) = map something . findPentagonal ((last ans) `mod` 100) $ ps
34+
where something (p, newAns) = (delete p ps, ans ++ [newAns])
35+
36+
myLast = iterate addOnePentagonal first !! 4
37+
38+
filtered = filter g myLast
39+
where g ([p], ans) = (completeCircle ans > 1000) && (completeCircle ans) `elemIncreasing` (poly p)
40+
41+
completeCircle ans = ((head ans) `div` 100) + ((last ans) `mod` 100 * 100)
42+
43+
main = print (sum l + completeCircle l)
44+
where l = snd . head $ filtered

0 commit comments

Comments
 (0)