Euler problems/1 to 10
From HaskellWiki
(Added problem_2_v3) |
(→Problem 5) |
||
| (42 intermediate revisions not shown.) | |||
| Line 1: | Line 1: | ||
| - | + | == [http://projecteuler.net/index.php?section=problems&id=1 Problem 1] == | |
| - | == [http://projecteuler.net/index.php?section= | + | |
Add all the natural numbers below 1000 that are multiples of 3 or 5. | Add all the natural numbers below 1000 that are multiples of 3 or 5. | ||
| - | + | Two solutions using <hask>sum</hask>: | |
<haskell> | <haskell> | ||
| - | problem_1 = sum [ x | x <- [1..999], | + | import Data.List (union) |
| + | problem_1' = sum (union [3,6..999] [5,10..999]) | ||
| + | |||
| + | problem_1 = sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0] | ||
</haskell> | </haskell> | ||
| + | |||
| + | Another solution which uses algebraic relationships: | ||
<haskell> | <haskell> | ||
| - | + | problem_1 = sumStep 3 999 + sumStep 5 999 - sumStep 15 999 | |
| + | where | ||
| + | sumStep s n = s * sumOnetoN (n `div` s) | ||
| + | sumOnetoN n = n * (n+1) `div` 2 | ||
</haskell> | </haskell> | ||
| - | |||
| - | |||
| - | |||
| - | + | == [http://projecteuler.net/index.php?section=problems&id=2 Problem 2] == | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | == [http://projecteuler.net/index.php?section= | + | |
Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million. | Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million. | ||
Solution: | Solution: | ||
<haskell> | <haskell> | ||
| - | problem_2 = sum [ x | x <- takeWhile (<= 1000000) fibs, x | + | problem_2 = sum [ x | x <- takeWhile (<= 1000000) fibs, even x] |
| - | where fibs = 1 : 1 : zipWith (+) fibs (tail fibs) | + | where |
| + | fibs = 1 : 1 : zipWith (+) fibs (tail fibs) | ||
</haskell> | </haskell> | ||
| Line 33: | Line 34: | ||
<hask>evenFib 0 = 0, evenFib 1 = 2, evenFib (n+2) = evenFib n + 4 * evenFib (n+1)</hask>. | <hask>evenFib 0 = 0, evenFib 1 = 2, evenFib (n+2) = evenFib n + 4 * evenFib (n+1)</hask>. | ||
<haskell> | <haskell> | ||
| - | + | problem_2 = sumEvenFibs $ numEvenFibsLessThan 1000000 | |
| - | sumEvenFibs n = (evenFib n + evenFib (n+1) - 2) `div` 4 | + | where |
| - | evenFib n = round $ (2 + sqrt 5) ** (fromIntegral n) / sqrt 5 | + | sumEvenFibs n = (evenFib n + evenFib (n+1) - 2) `div` 4 |
| - | numEvenFibsLessThan n = | + | evenFib n = round $ (2 + sqrt 5) ** (fromIntegral n) / sqrt 5 |
| - | + | numEvenFibsLessThan n = | |
| + | floor $ (log (fromIntegral n - 0.5) + 0.5*log 5) / log (2 + sqrt 5) | ||
</haskell> | </haskell> | ||
| Line 44: | Line 46: | ||
(up to at least 10^1000000 on my computer): | (up to at least 10^1000000 on my computer): | ||
<haskell> | <haskell> | ||
| - | + | problem_2 = sumEvenFibsLessThan 1000000 | |
| + | |||
sumEvenFibsLessThan n = (a + b - 1) `div` 2 | sumEvenFibsLessThan n = (a + b - 1) `div` 2 | ||
where | where | ||
n2 = n `div` 2 | n2 = n `div` 2 | ||
| - | (a, b) = foldr f (0,1) | + | (a, b) = foldr f (0,1) |
| + | . takeWhile ((<= n2) . fst) | ||
| + | . iterate times2E $ (1, 4) | ||
f x y | fst z <= n2 = z | f x y | fst z <= n2 = z | ||
| otherwise = y | | otherwise = y | ||
where z = x `addE` y | where z = x `addE` y | ||
| - | addE (a, b) (c, d) = | + | addE (a, b) (c, d) = (a*d + b*c - 4*ac, ac + b*d) |
| + | where ac=a*c | ||
| + | |||
times2E (a, b) = addE (a, b) (a, b) | times2E (a, b) = addE (a, b) (a, b) | ||
</haskell> | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=3 Problem 3] == |
Find the largest prime factor of 317584931803. | Find the largest prime factor of 317584931803. | ||
| Line 62: | Line 69: | ||
<haskell> | <haskell> | ||
primes = 2 : filter ((==1) . length . primeFactors) [3,5..] | primes = 2 : filter ((==1) . length . primeFactors) [3,5..] | ||
| + | |||
primeFactors n = factor n primes | primeFactors n = factor n primes | ||
| - | + | where | |
| - | + | factor n (p:ps) | |
| - | + | | p*p > n = [n] | |
| + | | n `mod` p == 0 = p : factor (n `div` p) (p:ps) | ||
| + | | otherwise = factor n ps | ||
problem_3 = last (primeFactors 317584931803) | problem_3 = last (primeFactors 317584931803) | ||
</haskell> | </haskell> | ||
| - | + | Another solution, not using recursion, is: | |
| - | < | + | <haskell> |
| - | + | problem_3 = (m !! 0) `div` (m !! 1) | |
| - | < | + | where |
| + | m = reverse $ | ||
| + | takeWhile (<=n) (scanl1 (*) [ x | x <- 2:[3,5..], (n `mod` x) == 0 ]) | ||
| + | n = 600851475143 | ||
| + | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=4 Problem 4] == |
Find the largest palindrome made from the product of two 3-digit numbers. | Find the largest palindrome made from the product of two 3-digit numbers. | ||
Solution: | Solution: | ||
<haskell> | <haskell> | ||
| - | problem_4 = | + | problem_4 = |
| - | + | maximum [x | y<-[100..999], z<-[y..999], let x=y*z, let s=show x, s==reverse s] | |
| - | + | ||
| - | + | ||
| - | + | ||
</haskell> | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=5 Problem 5] == |
What is the smallest number divisible by each of the numbers 1 to 20? | What is the smallest number divisible by each of the numbers 1 to 20? | ||
Solution: | Solution: | ||
<haskell> | <haskell> | ||
| - | problem_5 | + | problem_5 = foldr1 lcm [1..20] |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
</haskell> | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=6 Problem 6] == |
What is the difference between the sum of the squares and the square of the sums? | What is the difference between the sum of the squares and the square of the sums? | ||
Solution: | Solution: | ||
| + | <!-- | ||
<haskell> | <haskell> | ||
| - | + | fun n = a - b | |
| + | where | ||
| + | a=(n^2 * (n+1)^2) `div` 4 | ||
| + | b=(n * (n+1) * (2*n+1)) `div` 6 | ||
| + | |||
| + | problem_6 = fun 100 | ||
</haskell> | </haskell> | ||
| + | --> | ||
| + | <!-- Might just be me, but I find this a LOT easier to read. Perhaps not as good mathematically, but it runs in an instant, even for n = 25000. | ||
| + | <haskell> | ||
| + | fun n = a - b | ||
| + | where | ||
| + | a = (sum [1..n])^2 | ||
| + | b = sum (map (^2) [1..n]) | ||
| - | = | + | problem_6 = fun 100 |
| - | + | </haskell> | |
| - | + | --> | |
| - | + | <!-- I just made it a oneliner... --> | |
<haskell> | <haskell> | ||
| - | + | problem_6 = (sum [1..100])^2 - sum (map (^2) [1..100]) | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
</haskell> | </haskell> | ||
| - | + | == [http://projecteuler.net/index.php?section=problems&id=7 Problem 7] == | |
| - | + | Find the 10001st prime. | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | [http:// | + | |
| + | Solution: | ||
<haskell> | <haskell> | ||
| - | primes | + | --primes in problem_3 |
| - | + | problem_7 = primes !! 10000 | |
| - | + | ||
| - | + | ||
| - | + | ||
</haskell> | </haskell> | ||
| - | + | == [http://projecteuler.net/index.php?section=problems&id=8 Problem 8] == | |
| - | == [http://projecteuler.net/index.php?section= | + | |
Discover the largest product of five consecutive digits in the 1000-digit number. | Discover the largest product of five consecutive digits in the 1000-digit number. | ||
Solution: | Solution: | ||
| + | <!-- | ||
<haskell> | <haskell> | ||
| - | + | import Data.Char | |
| - | + | ||
| - | + | ||
groupsOf _ [] = [] | groupsOf _ [] = [] | ||
| - | groupsOf n xs = take n xs : groupsOf n ( tail xs ) | + | groupsOf n xs = |
| - | + | take n xs : groupsOf n ( tail xs ) | |
| - | problem_8 = maximum . map product . groupsOf 5 $ digits | + | |
| + | problem_8 x = maximum . map product . groupsOf 5 $ x | ||
| + | main = do t <- readFile "p8.log" | ||
| + | let digits = map digitToInt $concat $ lines t | ||
| + | print $ problem_8 digits | ||
| + | </haskell> | ||
| + | --> | ||
| + | <!-- I just cleaned up a little. --> | ||
| + | <haskell> | ||
| + | import Data.Char (digitToInt) | ||
| + | import Data.List (tails) | ||
| + | |||
| + | problem_8 = do str <- readFile "number.txt" | ||
| + | -- This line just converts our str(ing) to a list of 1000 Ints | ||
| + | let number = map digitToInt (concat $ lines str) | ||
| + | print $ maximum $ map (product . take 5) (tails number) | ||
</haskell> | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=9 Problem 9] == |
There is only one Pythagorean triplet, {''a'', ''b'', ''c''}, for which ''a'' + ''b'' + ''c'' = 1000. Find the product ''abc''. | There is only one Pythagorean triplet, {''a'', ''b'', ''c''}, for which ''a'' + ''b'' + ''c'' = 1000. Find the product ''abc''. | ||
Solution: | Solution: | ||
<haskell> | <haskell> | ||
| - | + | triplets l = [[a,b,c] | m <- [2..limit], | |
| - | + | n <- [1..(m-1)], | |
| + | let a = m^2 - n^2, | ||
| + | let b = 2*m*n, | ||
| + | let c = m^2 + n^2, | ||
| + | a+b+c==l] | ||
| + | where limit = floor . sqrt . fromIntegral $ l | ||
| - | + | problem_9 = product . head . triplets $ 1000 | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
</haskell> | </haskell> | ||
| - | == [http://projecteuler.net/index.php?section= | + | == [http://projecteuler.net/index.php?section=problems&id=10 Problem 10] == |
Calculate the sum of all the primes below one million. | Calculate the sum of all the primes below one million. | ||
Solution: | Solution: | ||
<haskell> | <haskell> | ||
| + | --primes in problem_3 | ||
problem_10 = sum (takeWhile (< 1000000) primes) | problem_10 = sum (takeWhile (< 1000000) primes) | ||
</haskell> | </haskell> | ||
| - | |||
| - | |||
| - | |||
| - | |||
Current revision
Contents |
1 Problem 1
Add all the natural numbers below 1000 that are multiples of 3 or 5.
Two solutions usingimport Data.List (union) problem_1' = sum (union [3,6..999] [5,10..999]) problem_1 = sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]
Another solution which uses algebraic relationships:
problem_1 = sumStep 3 999 + sumStep 5 999 - sumStep 15 999 where sumStep s n = s * sumOnetoN (n `div` s) sumOnetoN n = n * (n+1) `div` 2
2 Problem 2
Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million.
Solution:
problem_2 = sum [ x | x <- takeWhile (<= 1000000) fibs, even x] where fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
The following two solutions use the fact that the even-valued terms in the Fibonacci sequence themselves form a Fibonacci-like sequence that satisfies
problem_2 = sumEvenFibs $ numEvenFibsLessThan 1000000 where sumEvenFibs n = (evenFib n + evenFib (n+1) - 2) `div` 4 evenFib n = round $ (2 + sqrt 5) ** (fromIntegral n) / sqrt 5 numEvenFibsLessThan n = floor $ (log (fromIntegral n - 0.5) + 0.5*log 5) / log (2 + sqrt 5)
The first two solutions work because 10^6 is small. The following solution also works for much larger numbers (up to at least 10^1000000 on my computer):
problem_2 = sumEvenFibsLessThan 1000000 sumEvenFibsLessThan n = (a + b - 1) `div` 2 where n2 = n `div` 2 (a, b) = foldr f (0,1) . takeWhile ((<= n2) . fst) . iterate times2E $ (1, 4) f x y | fst z <= n2 = z | otherwise = y where z = x `addE` y addE (a, b) (c, d) = (a*d + b*c - 4*ac, ac + b*d) where ac=a*c times2E (a, b) = addE (a, b) (a, b)
3 Problem 3
Find the largest prime factor of 317584931803.
Solution:
primes = 2 : filter ((==1) . length . primeFactors) [3,5..] primeFactors n = factor n primes where factor n (p:ps) | p*p > n = [n] | n `mod` p == 0 = p : factor (n `div` p) (p:ps) | otherwise = factor n ps problem_3 = last (primeFactors 317584931803)
Another solution, not using recursion, is:
problem_3 = (m !! 0) `div` (m !! 1) where m = reverse $ takeWhile (<=n) (scanl1 (*) [ x | x <- 2:[3,5..], (n `mod` x) == 0 ]) n = 600851475143
4 Problem 4
Find the largest palindrome made from the product of two 3-digit numbers.
Solution:
problem_4 = maximum [x | y<-[100..999], z<-[y..999], let x=y*z, let s=show x, s==reverse s]
5 Problem 5
What is the smallest number divisible by each of the numbers 1 to 20?
Solution:
problem_5 = foldr1 lcm [1..20]
6 Problem 6
What is the difference between the sum of the squares and the square of the sums?
Solution:
problem_6 = (sum [1..100])^2 - sum (map (^2) [1..100])
7 Problem 7
Find the 10001st prime.
Solution:
--primes in problem_3 problem_7 = primes !! 10000
8 Problem 8
Discover the largest product of five consecutive digits in the 1000-digit number.
Solution:
import Data.Char (digitToInt) import Data.List (tails) problem_8 = do str <- readFile "number.txt" -- This line just converts our str(ing) to a list of 1000 Ints let number = map digitToInt (concat $ lines str) print $ maximum $ map (product . take 5) (tails number)
9 Problem 9
There is only one Pythagorean triplet, {a, b, c}, for which a + b + c = 1000. Find the product abc.
Solution:
triplets l = [[a,b,c] | m <- [2..limit], n <- [1..(m-1)], let a = m^2 - n^2, let b = 2*m*n, let c = m^2 + n^2, a+b+c==l] where limit = floor . sqrt . fromIntegral $ l problem_9 = product . head . triplets $ 1000
10 Problem 10
Calculate the sum of all the primes below one million.
Solution:
--primes in problem_3 problem_10 = sum (takeWhile (< 1000000) primes)
