Code style improvement
This commit is contained in:
parent
85841be19b
commit
327998ae0f
@ -1,11 +1,11 @@
|
||||
-- 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.
|
||||
sumMultiples :: (Integral n) => n
|
||||
|
||||
sumMultiples :: Integer
|
||||
sumMultiples = sum(filter p [ n | n <- [1..999] ])
|
||||
where p n = n `mod` 3 == 0 || n `mod` 5 == 0
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let result = sumMultiples
|
||||
putStrLn $ "Project Euler, Problem 1\n"
|
||||
|
@ -3,15 +3,15 @@
|
||||
-- 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.
|
||||
fib :: (Integral n) => n -> n
|
||||
|
||||
fib :: Integer -> Integer
|
||||
fib 0 = 0
|
||||
fib 1 = 1
|
||||
fib n = fib (n-1) + fib(n-2)
|
||||
|
||||
sumEvenFib :: (Integral n) => n
|
||||
sumEvenFib :: Integer
|
||||
sumEvenFib = sum $ filter even $ takeWhile (<=4000000) (map fib [0..])
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let result = sumEvenFib
|
||||
putStrLn $ "Project Euler, Problem 2\n"
|
||||
|
@ -1,14 +1,14 @@
|
||||
-- The prime factors of 13195 are 5, 7, 13 and 29. --
|
||||
-- What is the largest prime factor of the number 600851475143?
|
||||
import ProjectEuler
|
||||
|
||||
maxPrimeFactor :: (Integral n) => n -> n
|
||||
import ProjectEuler (isPrime)
|
||||
|
||||
maxPrimeFactor :: Integer -> Integer
|
||||
maxPrimeFactor n
|
||||
| isPrime n = n
|
||||
| n `mod` 2 == 0 = maxPrimeFactor $ fromIntegral n `div` 2
|
||||
| otherwise = maxPrimeFactor $ fromIntegral n `div` head [i | i <- [3,5..], n `mod` i == 0 && isPrime i]
|
||||
| n `mod` 2 == 0 = maxPrimeFactor $ n `div` 2
|
||||
| otherwise = maxPrimeFactor $ n `div` head [i | i <- [3,5..], n `mod` i == 0 && isPrime i]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let result = maxPrimeFactor 600851475143
|
||||
putStrLn $ "Project Euler, Problem 3\n"
|
||||
|
Loading…
x
Reference in New Issue
Block a user