Skip to content

Instantly share code, notes, and snippets.

@jnape
Created November 4, 2012 20:46
Show Gist options
  • Save jnape/4013713 to your computer and use it in GitHub Desktop.
Save jnape/4013713 to your computer and use it in GitHub Desktop.
Solution to Project Euler #1 in Haskell
module Euler1 where
{-
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.
-}
import Data.List (nub)
multiplesOf :: Int -> [Int] -> [Int]
multiplesOf n [] = []
multiplesOf n xs = [x | x <- xs, x % n]
(%) :: Int -> Int -> Bool
a % b = rem a b == 0
main :: IO ()
main = print $
let range = [1..999]
in sum $ nub $ multiplesOf 3 range ++ multiplesOf 5 range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment