diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-08-12 15:47:31 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-08-12 15:47:31 +0200 |
| commit | bb515e51d67f37ba9c6dfbd2fd0930be873a5ada (patch) | |
| tree | e86abea100d2c02a5dad7b6d5f0bf29c307bae04 | |
| parent | 1879caa1dd80cb11dd62403663917ad4bf7cc68e (diff) | |
| download | project_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.tar.gz project_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.tar.bz2 project_euler-bb515e51d67f37ba9c6dfbd2fd0930be873a5ada.zip | |
haskell problems 007 -> 010
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | haskell/007-10001st_prime.hs | 41 | ||||
| -rw-r--r-- | haskell/008-largest_product_in_a_series.hs | 64 | ||||
| -rw-r--r-- | haskell/009-special_pythagorean_triplet.hs | 21 | ||||
| -rw-r--r-- | haskell/010-summation_of_primes.hs | 16 | ||||
| -rw-r--r-- | python/010-summation_of_primes.py | 2 | ||||
| -rw-r--r-- | python/helper/__pycache__/prime.cpython-37.pyc | bin | 0 -> 1054 bytes | |||
| -rw-r--r-- | python/helper/data/names.txt (renamed from helper/data/names.txt) | 0 | ||||
| -rw-r--r-- | python/helper/data/words.txt (renamed from helper/data/words.txt) | 0 | ||||
| -rw-r--r-- | python/helper/numbers.py (renamed from helper/numbers.py) | 0 | ||||
| -rw-r--r-- | python/helper/other.py (renamed from helper/other.py) | 0 | ||||
| -rw-r--r-- | python/helper/palindrome.py (renamed from helper/palindrome.py) | 0 | ||||
| -rw-r--r-- | python/helper/prime.py (renamed from helper/prime.py) | 0 | ||||
| -rw-r--r-- | python/helper/tree.py (renamed from helper/tree.py) | 0 |
14 files changed, 144 insertions, 2 deletions
@@ -2,4 +2,4 @@ My attempt at solving some of the [Project Euler](https://projecteuler.net/) problems. -This is a mess btw but the solutions are in c/ and python/. +This is a mess btw but the solutions are in [c/](c/), [python/](python/) and [haskell/](haskell/). 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] diff --git a/python/010-summation_of_primes.py b/python/010-summation_of_primes.py index e637c9c..0f296d2 100644 --- a/python/010-summation_of_primes.py +++ b/python/010-summation_of_primes.py @@ -2,7 +2,7 @@ from helper.prime import is_prime prime_numbers = [] for num in range(2, 2_000_000): - if is_prime(num, prime_numbers): + if is_prime(num): prime_numbers.append(num) num += 1 diff --git a/python/helper/__pycache__/prime.cpython-37.pyc b/python/helper/__pycache__/prime.cpython-37.pyc Binary files differnew file mode 100644 index 0000000..bf9e3d6 --- /dev/null +++ b/python/helper/__pycache__/prime.cpython-37.pyc diff --git a/helper/data/names.txt b/python/helper/data/names.txt index 7b8986b..7b8986b 100644 --- a/helper/data/names.txt +++ b/python/helper/data/names.txt diff --git a/helper/data/words.txt b/python/helper/data/words.txt index 7177624..7177624 100644 --- a/helper/data/words.txt +++ b/python/helper/data/words.txt diff --git a/helper/numbers.py b/python/helper/numbers.py index f99f205..f99f205 100644 --- a/helper/numbers.py +++ b/python/helper/numbers.py diff --git a/helper/other.py b/python/helper/other.py index 05854d8..05854d8 100644 --- a/helper/other.py +++ b/python/helper/other.py diff --git a/helper/palindrome.py b/python/helper/palindrome.py index 3c498ad..3c498ad 100644 --- a/helper/palindrome.py +++ b/python/helper/palindrome.py diff --git a/helper/prime.py b/python/helper/prime.py index 1107c7c..1107c7c 100644 --- a/helper/prime.py +++ b/python/helper/prime.py diff --git a/helper/tree.py b/python/helper/tree.py index 620f99b..620f99b 100644 --- a/helper/tree.py +++ b/python/helper/tree.py |
