在Haskell中项目Euler Number 10,找不到Bug

Bra*_*ons 1 haskell

我是Haskell的初学者,我遇到了Project Euler Problem 10的问题.这就是我所拥有的:

primes :: Integral a => [a]
primes = filter isPrime [1,3..]

isPrime :: Integral a => a -> Bool
isPrime 1 = False
isPrime n = not $ any isDivisibleBy [2..maxTry]
    where isDivisibleBy x = n `mod` x == 0
          maxTry = floor $ sqrt $ fromIntegral n

solution :: Integral a => a
solution = sum $ takeWhile (<2000000) primes

main = putStrLn $ show solution
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到142913828920.Euler项目说这是不正确的.这是莫名其妙的我,因为我成功地使用了相同的定义,primesisPrime在第7题,这是找到第一万零一素.救命?

Don*_*oby 5

我会质疑这条线

primes = filter isPrime [1,3..]
Run Code Online (Sandbox Code Playgroud)

因为2是素数.

  • 并且1不是素数,除非你是[世界上最精英的黑客](http://www.imdb.com/title/tt0298814/goofs) (2认同)