我是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项目说这是不正确的.这是莫名其妙的我,因为我成功地使用了相同的定义,primes并isPrime在第7题,这是找到第一万零一素.救命?
我会质疑这条线
primes = filter isPrime [1,3..]
Run Code Online (Sandbox Code Playgroud)
因为2是素数.
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |