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]
Question
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]
Solution
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]
OTHER TIPS
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
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.