Permalink
Browse files

reimplement euler001 as infinite list processing

  • Loading branch information...
1 parent ca00c5e commit 318a61caa34887ac83517e2fa42b6c83837be9ff @youknowone committed Mar 27, 2011
Showing with 22 additions and 13 deletions.
  1. +14 −0 euler001.cycle.hs
  2. +8 −13 euler001.hs
View
@@ -0,0 +1,14 @@
+
+module Main where
+
+import System.IO
+
+sum3_5 0 = 0
+sum3_5 n
+ | n `mod` 3 == 0 = n + sum3_5(n-1)
+ | n `mod` 5 == 0 = n + sum3_5(n-1)
+ | otherwise = sum3_5(n-1)
+
+main :: IO()
+main = do
+ print (sum3_5 999)
View
@@ -1,14 +1,9 @@
-module Main where
-
-import System.IO
-
-sum3_5 0 = 0
-sum3_5 n
- | n `mod` 3 == 0 = n + sum3_5(n-1)
- | n `mod` 5 == 0 = n + sum3_5(n-1)
- | otherwise = sum3_5(n-1)
-
-main :: IO()
-main = do
- print (sum3_5 999)
+sum3_5 n = [x|x <- [1..], mod x 3 == 0 || mod x 5 == 0] `sum_to` n
+ where
+ sum_to (x:xs) n
+ | x >= n = 0
+ | otherwise = x + (xs `sum_to` n)
+
+main = do
+ print $ sum3_5 1000

0 comments on commit 318a61c

Please sign in to comment.