Skip to content

Commit e75376c

Browse files
committed
BLD added solutions 1-4
1 parent bee5cf3 commit e75376c

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

euler001.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main = print $ sum $ filter multiple3or5 [1..999]
2+
where multiple3or5 = (\n -> n `rem` 3 == 0 || n `rem` 5 == 0)

euler002.hs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Data.List
2+
3+
main = print $ sum [x | x <- takeWhile (< 4000000) fibs, even x]
4+
where fibs = unfoldr (\(a, b) -> Just (a, (b, a+b))) (1, 1)
5+
6+
{-
7+
It turns out there is an extremely elegant, recursive way of generating the
8+
Fibonacci numbers using zipWith:
9+
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
10+
-}

euler003.hs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
main = print $ maximum $ prime_factors 600851475143
2+
3+
prime_factors :: (Integral a) => a -> [a]
4+
prime_factors 1 = []
5+
prime_factors n
6+
| factor == [] = [n]
7+
| n < (head factor)^2 = [n] -- stop searching if sqrt n < head factor
8+
| otherwise = factor ++ prime_factors (n `div` (head factor))
9+
where factor = take 1 [x | x <- [2..n-1], n `rem` x == 0]

euler004.hs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
main = print $ maximum palindromes
2+
where palindromes = [x*y | x <- [999,998..1], y <- [999,998..1],
3+
(reverse $ show (x*y)) == (show (x*y))]

0 commit comments

Comments
 (0)