Prime factor of 300 000 000 000?

前端 未结 19 778
无人及你
无人及你 2021-01-03 10:23

I need to find out the prime factors of over 300 billion. I have a function that is adding to the list of them...very slowly! It has been running for about an hour now and i

19条回答
  •  死守一世寂寞
    2021-01-03 11:13

    Here's some Haskell goodness for you guys :)

    primeFactors n = factor n primes
      where factor n (p:ps) | p*p > n = [n]
                            | n `mod` p /= 0 = factor n ps
                            | otherwise = p : factor (n `div` p) (p:ps)
            primes = 2 : filter ((==1) . length . primeFactors) [3,5..]
    

    Took it about .5 seconds to find them, so I'd call that a success.

提交回复
热议问题