aboutsummaryrefslogtreecommitdiff
path: root/haskell
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-12 15:47:31 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-12 15:47:31 +0200
commitbb515e51d67f37ba9c6dfbd2fd0930be873a5ada (patch)
treee86abea100d2c02a5dad7b6d5f0bf29c307bae04 /haskell
parent1879caa1dd80cb11dd62403663917ad4bf7cc68e (diff)
downloadproject_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.tar.gz
project_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.tar.bz2
project_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.zip
haskell problems 007 -> 010
Diffstat (limited to 'haskell')
-rw-r--r--haskell/007-10001st_prime.hs41
-rw-r--r--haskell/008-largest_product_in_a_series.hs64
-rw-r--r--haskell/009-special_pythagorean_triplet.hs21
-rw-r--r--haskell/010-summation_of_primes.hs16
4 files changed, 142 insertions, 0 deletions
diff --git a/haskell/007-10001st_prime.hs b/haskell/007-10001st_prime.hs
new file mode 100644
index 0000000..c775f7d
--- /dev/null
+++ b/haskell/007-10001st_prime.hs
@@ -0,0 +1,41 @@
+-- 10001st prime
+
+-- Problem 7
+-- By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
+-- we can see that the 6th prime is 13.
+
+-- What is the 10 001st prime number?
+
+
+main = do
+ print ([x | x <- [1..150000], is_prime x] !! 10000) -- ugly and inefficient
+ print (eratos_sieve [2..150000] !! 10000) -- still ugly and inefficient due
+ -- to the arbirary range
+ print (nth_prime 10000) -- better but meh since we're testing every divisor each time,
+ -- should keep a list of them
+
+nth_prime :: Int -> Int
+nth_prime n = nth_check n 0
+ where nth_check n x
+ | is_prime x = if n == 0 then x else nth_check (n - 1) (x + 1)
+ | otherwise = nth_check n (x + 1)
+
+eratos_sieve :: [Int] -> [Int]
+eratos_sieve [] = []
+eratos_sieve (x:xs)
+ | x * x > last xs = x:xs
+ | otherwise = x:eratos_sieve [n | n <- xs, n `mod` x /= 0]
+
+is_prime :: Int -> Bool
+is_prime 0 = False
+is_prime 1 = False
+is_prime 2 = True
+is_prime 3 = True
+is_prime x
+ | x `mod` 2 == 0 || x `mod` 3 == 0 = False
+ | otherwise = trial_div 5
+ where trial_div d
+ | d * d > x = True
+ | x `mod` d == 0 || x `mod` (d + 2) == 0 = False
+ | otherwise = trial_div (d + 6)
+
diff --git a/haskell/008-largest_product_in_a_series.hs b/haskell/008-largest_product_in_a_series.hs
new file mode 100644
index 0000000..6d3a80e
--- /dev/null
+++ b/haskell/008-largest_product_in_a_series.hs
@@ -0,0 +1,64 @@
+-- Largest product in a series
+
+-- Problem 8
+-- The four adjacent digits in the 1000-digit number that have
+-- the greatest product are 9 × 9 × 8 × 9 = 5832.
+
+-- 73167176531330624919225119674426574742355349194934
+-- 96983520312774506326239578318016984801869478851843
+-- 85861560789112949495459501737958331952853208805511
+-- 12540698747158523863050715693290963295227443043557
+-- 66896648950445244523161731856403098711121722383113
+-- 62229893423380308135336276614282806444486645238749
+-- 30358907296290491560440772390713810515859307960866
+-- 70172427121883998797908792274921901699720888093776
+-- 65727333001053367881220235421809751254540594752243
+-- 52584907711670556013604839586446706324415722155397
+-- 53697817977846174064955149290862569321978468622482
+-- 83972241375657056057490261407972968652414535100474
+-- 82166370484403199890008895243450658541227588666881
+-- 16427171479924442928230863465674813919123162824586
+-- 17866458359124566529476545682848912883142607690042
+-- 24219022671055626321111109370544217506941658960408
+-- 07198403850962455444362981230987879927244284909188
+-- 84580156166097919133875499200524063689912560717606
+-- 05886116467109405077541002256983155200055935729725
+-- 71636269561882670428252483600823257530420752963450
+
+-- Find the thirteen adjacent digits in the 1000-digit number that have
+-- the greatest product. What is the value of this product?
+
+
+import Data.Char
+
+main = do
+ print (largest_adj_product big_nb)
+
+largest_adj_product :: String -> Int
+largest_adj_product str = find_max str 0
+ where find_max s m
+ | length s < 13 = m
+ | (first_prod s) > m = find_max (tail s) (first_prod s)
+ | otherwise = find_max (tail s) m
+ first_prod striped = foldl (\acc x -> acc * (digitToInt x)) 1 (take 13 striped)
+
+big_nb = "73167176531330624919225119674426574742355349194934\
+ \96983520312774506326239578318016984801869478851843\
+ \85861560789112949495459501737958331952853208805511\
+ \12540698747158523863050715693290963295227443043557\
+ \66896648950445244523161731856403098711121722383113\
+ \62229893423380308135336276614282806444486645238749\
+ \30358907296290491560440772390713810515859307960866\
+ \70172427121883998797908792274921901699720888093776\
+ \65727333001053367881220235421809751254540594752243\
+ \52584907711670556013604839586446706324415722155397\
+ \53697817977846174064955149290862569321978468622482\
+ \83972241375657056057490261407972968652414535100474\
+ \82166370484403199890008895243450658541227588666881\
+ \16427171479924442928230863465674813919123162824586\
+ \17866458359124566529476545682848912883142607690042\
+ \24219022671055626321111109370544217506941658960408\
+ \07198403850962455444362981230987879927244284909188\
+ \84580156166097919133875499200524063689912560717606\
+ \05886116467109405077541002256983155200055935729725\
+ \71636269561882670428252483600823257530420752963450"
diff --git a/haskell/009-special_pythagorean_triplet.hs b/haskell/009-special_pythagorean_triplet.hs
new file mode 100644
index 0000000..056a7c3
--- /dev/null
+++ b/haskell/009-special_pythagorean_triplet.hs
@@ -0,0 +1,21 @@
+-- Special Pythagorean triplet
+--
+-- Problem 9
+-- A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
+--
+-- a^2 + b^2 = c^2
+-- For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
+--
+-- There exists exactly one Pythagorean triplet for which a + b + c = 1000.
+-- Find the product abc.
+
+
+main = do
+ print (truncate (pyth_triplet 1 1))
+
+pyth_triplet :: Float -> Float -> Float
+pyth_triplet a b
+ | a + b + c == 1000 = a * b * c
+ | b >= 1000 = pyth_triplet (a + 1) 0
+ | otherwise = pyth_triplet a (b + 1)
+ where c = sqrt (a ^ 2 + b ^ 2)
diff --git a/haskell/010-summation_of_primes.hs b/haskell/010-summation_of_primes.hs
new file mode 100644
index 0000000..e8d8015
--- /dev/null
+++ b/haskell/010-summation_of_primes.hs
@@ -0,0 +1,16 @@
+-- Summation of primes
+
+-- Problem 10
+-- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
+
+-- Find the sum of all the primes below two million.
+
+
+main = do
+ print (sum $ eratos_sieve [2..1999999])
+
+eratos_sieve :: [Int] -> [Int]
+eratos_sieve [] = []
+eratos_sieve (x:xs)
+ | x * x > last xs = x:xs
+ | otherwise = x:eratos_sieve [n | n <- xs, n `mod` x /= 0]