Created
November 24, 2012 04:49
-
-
Save edalorzo/4138441 to your computer and use it in GitHub Desktop.
Project Euler - Problem #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Euler.Problem1 where | |
-- | Given a maximum integer m and a factor n calculates the sum | |
-- of all divisors of the given factor up to m. | |
sumDivisible :: Int -> Int -> Int | |
sumDivisible m n = n * (p * (p + 1)) `div` 2 where p = m `div` n | |
result = (sumDivisible 999 3) + (sumDivisible 999 5) - (sumDivisible 999 15) | |
solve::String | |
solve = ("The sum of multiples of 3 and 5 up to 1000 is: " ++ show result) | |
-- | A naive solution that given two multiples j and k and maximum integer m | |
--- calculates the sum of all multiples of j and k. | |
alternative :: Int -> Int -> Int -> Int | |
alternative j k m = sum [x | x <- [1..m-1], x `mod` j == 0 || x `mod` k == 0] | |
main = putStrLn solve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment