[Haskell-cafe] For Project Euler #1 isn't it more efficient to generate just the numbers you need? <Spoiler>

Lyndon Maydwell maydwell at gmail.com
Sat May 7 07:10:54 CEST 2011


If you're looking for efficiency, I believe you can actually do #1 in
constant time:


On Sat, May 7, 2011 at 7:31 AM,  <caseyh at istar.ca> wrote:
> -- Instead of this
> -- sumMultiples3or5 s = sum [x | x <- [3..s-1], x `mod` 3 == 0 || x `mod` 5
> == 0]
>
>
> -- Isn't this faster
>
> sumMultiples3or5 s = sum ([x | x <- [3,6..s-1]] `merge` [x | x <-
> [5,10..s-1]])
>
> merge xs [] = xs
> merge [] ys = ys
> merge txs@(x:xs) tys@(y:ys)
>    | x < y     = x : xs `merge` tys
>    | x > y     = y : txs `merge` ys
>    | otherwise = x : xs `merge` ys
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list