From 32f83b946f11efc2f72b6fe78247b6f7b611893a Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Mon, 10 Oct 2011 22:57:41 -0400 Subject: [PATCH] Haskell 001 --- haskell/e001.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 haskell/e001.hs diff --git a/haskell/e001.hs b/haskell/e001.hs new file mode 100644 index 0000000..7e0d147 --- /dev/null +++ b/haskell/e001.hs @@ -0,0 +1,20 @@ +{- Add all the natural numbers below one thousand that are multiples of 3 or 5. + + 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 Text.Printf + +multiples :: (Integral a) => a -> a +multiples max = + if max < 3 then + 0 + else + if (max `mod` 5 == 0 || max `mod` 3 == 0) then + max + multiples (max - 1) + else multiples (max - 1) + +main = do + printf "Sum of multiples below 10: %d\n" (multiples 9 :: Int) + printf "Sum of multiples below 1000: %d\n" (multiples 999 :: Int)