Permalink
Browse files

added comments

  • Loading branch information...
1 parent a5f22a2 commit 278d919c26329dc9559aceb25a8dfe747da72971 Jan Minárik committed May 3, 2012
Showing with 66 additions and 6 deletions.
  1. +66 −6 00.hs
View
72 00.hs
@@ -1,18 +1,54 @@
import Control.Monad
import Data.List
---1--
+-----------------------------------------------------------
+--
+-- 1
+--
+-- If we list all the natural numbers below 10 that are
+-- multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of
+-- these multiples is 23.
+--
+-- Find the sum of all the multiples of 3 or 5 below 1000.
+--
+-----------------------------------------------------------
+
eu1 = sum [x | x <- [1..999], any (isFactor x) [5,3]]
isFactor :: Integral a => a -> a -> Bool
isFactor a b = a `mod` b == 0
---2--
+-----------------------------------------------------------
+--
+-- 2
+--
+-- Each new term in the Fibonacci sequence is generated by
+-- adding the previous two terms. By starting with 1 and 2,
+-- the first 10 terms will be:
+--
+-- 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
+--
+-- By considering the terms in the Fibonacci sequence whose
+-- values do not exceed four million, find the sum of the
+-- even-valued terms.
+--
+-----------------------------------------------------------
+
eu2 = sum fibs
where nextFib (a,b,c) = (b,c,b+c)
fibs = takeWhile (< 4000000) [x | (_,_,x) <- iterate nextFib (0,1,1), even x]
---3--
+-----------------------------------------------------------
+--
+-- 3
+--
+-- The prime factors of 13195 are 5, 7, 13 and 29.
+--
+-- What is the largest prime factor of the number
+-- 600851475143 ?
+--
+-----------------------------------------------------------
+
eu3 x = primeFactor x (squareRoot x)
primeFactor :: Integral a => a -> a -> a
@@ -26,7 +62,19 @@ squareRoot = floor . sqrt . (fromIntegral :: (Integral a) => a -> Double)
isPrime :: (Integral a) => a -> Bool
isPrime a = all (not . isFactor a) [2..(squareRoot a)]
---4--
+-----------------------------------------------------------
+--
+-- 4
+--
+-- A palindromic number reads the same both ways. The
+-- largest palindrome made from the product of two 2-digit
+-- numbers is 9009 = 91 99.
+--
+-- Find the largest palindrome made from the product of two
+-- 3-digit numbers.
+--
+-----------------------------------------------------------
+
eu4 = maximum palindromes
where
palindromes = do
@@ -36,7 +84,18 @@ eu4 = maximum palindromes
guard (show p == (reverse . show $ p))
return p
---5--
+-----------------------------------------------------------
+--
+-- 5
+--
+-- 2520 is the smallest number that can be divided by each
+-- of the numbers from 1 to 10 without any remainder.
+--
+-- What is the smallest positive number that is evenly
+-- divisible by all of the numbers from 1 to 20?
+--
+-----------------------------------------------------------
+
eu5 = foldl1 (*) . foldl1 merge $ map factors [2..20]
merge :: (Eq a, Ord a) => [a] -> [a] -> [a]
@@ -55,4 +114,5 @@ factors :: (Integral a) => a -> [a]
factors x
| isPrime x = [x]
| otherwise = p:(factors (x `div` p))
- where p = primeFactor x (squareRoot x)
+ where p = primeFactor x (squareRoot x)
+

0 comments on commit 278d919

Please sign in to comment.