This is my code:
import Data.Bits
main = print . sum . takeWhile( < 200000) $ multSum 999
multSum m = 3 : multiples [6..m] where
multiples (p:xs)
| ((p `mod` 3 == 0) || (p `mod` 5 == 0)) = p : multiples([p..m])
| otherwise = p : xs
Error: out of memory (requested 1048576 bytes)
Where am I going wrong?
multSum
isn't doing what you think it is. Try debugging it directly:
*Main> take 20 $ multSum 999
[3,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6]
multSum
为所有参数返回无限列表[3,6,6,6,6...]
,因此它永远不会超过 200000,因此无法打印您请求的总和。
try
mults35 m = multiples [3..m] .....
............
....| ...... = p : multiples xs
... | otherwise = multiples xs
there will be one more thing for you to add there. Try this, and you'll see.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.