简体   繁体   English

我对Euler#3的Haskell解决方案效率低下

[英]My Haskell Solution to Euler #3 is Inefficient

I am attempting to solve Euler problem 3 in Haskell, which involves finding the largest prime factor of a number. 我正在尝试解决Haskell中的Euler问题3,该问题涉及找到数的最大素数。 My code runs for a long time and seems to hang. 我的代码运行了很长时间,并且似乎挂起了。 What is causing my code to be so grossly inefficient? 是什么导致我的代码如此低效?

primes = sieve (2:[3,5..])
 where sieve (x:xs) = x:[y | y <- (sieve xs), mod y x /= 0]
       sieve [] = []
primefactors n = filter (\x -> mod n x == 0) (primesUnder n)
 where primesUnder z = reverse (takeWhile (< z) primes)
solve3 = head (primefactors 600851475143)

Your main problem is you're checking for enormous primes -- all the way up to 600851475143 . 您的主要问题是要检查巨大的素数-一直到600851475143 You can improve things a lot by observing two things: 您可以通过观察以下两点来极大地改善事情:

  1. Every time you find a prime, you can decrease the maximum prime you look at by dividing away that factor. 每次找到素数时,都可以通过除以该因子来减少最大素数。
  2. You only have to look for primes until you reach the square root of the target. 您只需要寻找素数,直到达到目标的平方根。 If your primes are bigger than that, and you know there are no smaller factors, you're done. 如果素数大于此,并且您知道没有更小的因子,那么您就完成了。

Using these two improvements together, even without the nicety that you used of only checking primes for divisibility, makes the program run in a snap: 结合使用这两项改进,即使没有只检查素数是否可除的优点,该程序也可以迅速运行:

factor = go (2:[3,5..]) where
    go (p:ps) n
        | p*p > n        = [n]
        | n `mod` p == 0 = p : go (p:ps) (n `div` p)
        | otherwise      = go ps n
main = print . last . factor $ 600851475143

In ghci: 在ghci中:

*Main> main
6857
(0.00 secs, 0 bytes)

You can see that we only had to inspect numbers up to 6857 -- eight orders of magnitude smaller than what you would have to do with your approach. 您可以看到,我们只需要检查最多6857个数字-比您的方法要小八个数量级。

Independently, your sieve is dog slow. 独立地,您的筛子太慢了。 You could have a look at the wiki for ideas about how to find primes quickly. 您可以在Wiki上查看有关如何快速查找素数的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM