Skip to content

Instantly share code, notes, and snippets.

@edalorzo
Created November 24, 2012 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edalorzo/4138441 to your computer and use it in GitHub Desktop.
Save edalorzo/4138441 to your computer and use it in GitHub Desktop.
Project Euler - Problem #1
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