Add basic Project Euler solutions
This commit is contained in:
10
project-euler-haskell/problem1.hs
Normal file
10
project-euler-haskell/problem1.hs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn :: Integer -> Integer
|
||||
fn n | n < 3 = 0
|
||||
| n == 3 = 3
|
||||
| n `rem` 3 == 0 || n `rem` 5 == 0 = n + fn (n-1)
|
||||
| otherwise = fn (n-1)
|
||||
main = do
|
||||
putStrLn "The sum of the multiples of 3 and 5 from 1 to 1000 is:"
|
||||
print (fn 1000)
|
||||
|
||||
-- filter (\n -> n \`rem\` 3 == 0)
|
||||
10
project-euler-haskell/problem2.hs
Normal file
10
project-euler-haskell/problem2.hs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn :: Integer -> Integer -> Integer -> Integer
|
||||
fn m n max | m == 0 = fn n 1 max
|
||||
| sum >= max = 0
|
||||
| even sum = sum + fn n sum max
|
||||
| odd sum = fn n sum max
|
||||
| otherwise = 0
|
||||
where sum = m+n
|
||||
main = do
|
||||
putStrLn "The sum of the even Fibonacci numbers below 4000 is:"
|
||||
print (fn 0 0 4000)
|
||||
10
project-euler-haskell/problem3.hs
Normal file
10
project-euler-haskell/problem3.hs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn :: Integer -> Integer -> Integer
|
||||
fn n fact | fact == 0 = fn n 2
|
||||
| n `rem` fact == 0 = fn div fact
|
||||
| fact <= 2 && fact <= div = fn n (fact+1)
|
||||
| fact <= div = fn n (fact+2)
|
||||
| otherwise = n
|
||||
where div = n `quot` fact
|
||||
main = do
|
||||
putStrLn "The largest prime factor of 600851475143 is:"
|
||||
print (fn 600851475143 0)
|
||||
Reference in New Issue
Block a user