empty list on recursive function

烂漫一生 提交于 2019-12-14 02:35:59

问题


Im trying do some work on Project Euler for fun, but I got stuck now on an problem and want to move on but cant seem to get my function working. Im trying to get count the primefactors of a given integer. The function works on smaller numbers such as 13195:

> primeFactor 13195
[29,13,7,5]

But when I run a bigger number such as 600851475143:

> primeFactor 601851475143
[] 

This seems really weird to me. I know haskell is a lazy language, but I don´t think it should be that lazy...

primeFactor' :: Int -> [Int]
primeFactor' n = [ p | p <- primes' [] [2 .. n], n `mod` p == 0 ]
   where primes' :: [Int] -> [Int] -> [Int]
         primes' ys [] = ys
         primes' ys (x:xs) | x `notMultipleOf` ys = primes' (x:ys) xs
                           | otherwise            = primes' ys xs                                                                                         

-- helper function to primeFactor'
notMultipleOf :: Int -> [Int] -> Bool
notMultipleOf n [] = True
notMultipleOf n xs = and [n `mod` x /= 0 | x <- xs]

回答1:


Int has 32 bits you can't store that number (use Integer).

On the other hand, you can use Data.Numbers.Primes (and see code):

> primeFactors 601851475143
[3,3,23,1009,2881561]
> primeFactors 600851475143
[71,839,1471,6857]



回答2:


It's an integer overflow error. The Int type can only represent numbers up to 2^31 - 1

>> maxBound :: Int
2147483647

The number you entered overflows --

>> 601851465143 :: Int
556043703

On the other hand, if you use the Integer type there is no overflow --

>> 601851465143 :: Integer
601851465143



回答3:


Honestly, I don't know why you obtained an empty list.... or anything at all.

You are using a brute force method to find a list of primes, dividing all numbers by all smaller primes than it. This scales like n^2.

How long should this take to complete?

N^2 ~= (601851475143)^2 ~= 10^22 operations

It is a bit better than this, because the density of primes drops, but not much.... Let's shave off a factor of 10^2 to account for this. On a modern 8 core 4GHz machine (assuming 1cpu cycle per operation.... way optimistic), this should take

10^20 operations / (10^10 operation/sec) = 10^10 sec ~= 300 years

to complete.

You might want to look for a new algorithm.



来源:https://stackoverflow.com/questions/20700233/empty-list-on-recursive-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!