Skip to content

Commit

Permalink
reimplement euler001 as infinite list processing
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Mar 27, 2011
1 parent ca00c5e commit 318a61c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
14 changes: 14 additions & 0 deletions euler001.cycle.hs
@@ -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)
21 changes: 8 additions & 13 deletions euler001.hs
@@ -1,14 +1,9 @@


module Main where sum3_5 n = [x|x <- [1..], mod x 3 == 0 || mod x 5 == 0] `sum_to` n

where
import System.IO sum_to (x:xs) n

| x >= n = 0
sum3_5 0 = 0 | otherwise = x + (xs `sum_to` n)
sum3_5 n
| n `mod` 3 == 0 = n + sum3_5(n-1) main = do
| n `mod` 5 == 0 = n + sum3_5(n-1) print $ sum3_5 1000
| otherwise = sum3_5(n-1)

main :: IO()
main = do
print (sum3_5 999)

0 comments on commit 318a61c

Please sign in to comment.