aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--haskell/001-multiples_of_3_and_5.hs10
-rw-r--r--haskell/002-even_fibonacci_numbers.hs26
-rw-r--r--haskell/003-largest_prime_factor.hs19
-rw-r--r--haskell/004-largest_palindrome_product.hs18
-rw-r--r--haskell/005-smallest_multiple.hs23
-rw-r--r--haskell/006-sum_square_difference.hs18
7 files changed, 118 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cf20380
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.o
+a.out
+haskell/*
+!haskell/*.hs
diff --git a/haskell/001-multiples_of_3_and_5.hs b/haskell/001-multiples_of_3_and_5.hs
new file mode 100644
index 0000000..f99af1e
--- /dev/null
+++ b/haskell/001-multiples_of_3_and_5.hs
@@ -0,0 +1,10 @@
+-- Multiples of 3 and 5
+
+-- Problem 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.
+
+
+main = do
+ print (sum [x | x <- [0..999], x `mod` 3 == 0 || x `mod` 5 == 0])
diff --git a/haskell/002-even_fibonacci_numbers.hs b/haskell/002-even_fibonacci_numbers.hs
new file mode 100644
index 0000000..ff5de91
--- /dev/null
+++ b/haskell/002-even_fibonacci_numbers.hs
@@ -0,0 +1,26 @@
+-- Even Fibonacci numbers
+
+-- Problem 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.
+
+
+main = do
+ print (sum (filter even (fib_until 4000000)))
+
+fib_until threshold = fib_from_index_until 0 threshold
+
+fib_from_index_until :: Int -> Int -> [Int]
+fib_from_index_until i threshold
+ | fib_i < threshold = fib_i:(fib_from_index_until (i + 1) threshold)
+ | otherwise = []
+ where fib_i = fibonacci i
+
+fibonacci :: Int -> Int
+fibonacci 0 = 0
+fibonacci 1 = 1
+fibonacci 2 = 1
+fibonacci x = fibonacci (x - 1) + fibonacci (x - 2)
diff --git a/haskell/003-largest_prime_factor.hs b/haskell/003-largest_prime_factor.hs
new file mode 100644
index 0000000..4150377
--- /dev/null
+++ b/haskell/003-largest_prime_factor.hs
@@ -0,0 +1,19 @@
+-- Largest prime factor
+
+-- Problem 3
+-- The prime factors of 13195 are 5, 7, 13 and 29.
+
+-- What is the largest prime factor of the number 600851475143 ?
+
+
+main = do
+ print (last $ factors 600851475143)
+
+factors x = trial_division 2 x
+
+trial_division :: Int -> Int -> [Int]
+trial_division by x
+ | x <= 1 = []
+ | x `mod` by == 0 = by:trial_division by (x `div` by)
+ | otherwise = trial_division (by + 1) x
+
diff --git a/haskell/004-largest_palindrome_product.hs b/haskell/004-largest_palindrome_product.hs
new file mode 100644
index 0000000..e0bd77d
--- /dev/null
+++ b/haskell/004-largest_palindrome_product.hs
@@ -0,0 +1,18 @@
+-- Largest palindrome product
+
+-- Problem 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.
+
+
+main = do
+ print (maximum [x * y | x <- [0..999], y <- [0..999], is_palindrome (x * y)])
+
+is_palindrome :: Int -> Bool
+is_palindrome x = all (\(y, z) -> y == z) $ zip (show x) (rev' $ show x)
+
+rev' :: [a] -> [a]
+rev' [] = []
+rev' (x:xs) = (rev' xs) ++ [x]
diff --git a/haskell/005-smallest_multiple.hs b/haskell/005-smallest_multiple.hs
new file mode 100644
index 0000000..c395eed
--- /dev/null
+++ b/haskell/005-smallest_multiple.hs
@@ -0,0 +1,23 @@
+-- Smallest multiple
+
+-- Problem 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?
+
+
+main = do
+ print (first_divisible 2520 [1..20]) -- takes 16s
+
+first_divisible :: Int -> [Int] -> Int
+first_divisible x divs
+ | divisible x divs = x
+ | otherwise = first_divisible (x + 2) divs
+
+divisible :: Int -> [Int] -> Bool
+divisible _ [] = True
+divisible x (d:divs)
+ | x `mod` d /= 0 = False
+ | otherwise = divisible x divs
diff --git a/haskell/006-sum_square_difference.hs b/haskell/006-sum_square_difference.hs
new file mode 100644
index 0000000..3c277af
--- /dev/null
+++ b/haskell/006-sum_square_difference.hs
@@ -0,0 +1,18 @@
+-- Sum square difference
+
+-- Problem 6
+-- The sum of the squares of the first ten natural numbers is,
+
+-- 1^2 + 2^2 + ... + 10^2 = 385
+-- The square of the sum of the first ten natural numbers is,
+
+-- (1 + 2 + ... + 10)^2 = 55^2 = 3025
+-- Hence the difference between the sum of the squares of the first ten natural
+-- numbers and the square of the sum is 3025 − 385 = 2640.
+
+-- Find the difference between the sum of the squares of the first one hundred
+-- natural numbers and the square of the sum.
+
+
+main = do
+ print (sum [1..100] ^ 2 - sum (map (^2) [1..100]))