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]
Int
has 32 bits you can't store that number (useInteger
).On the other hand, you can use
Data.Numbers.Primes
(and see code):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?
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
to complete.
You might want to look for a new algorithm.
It's an integer overflow error. The
Int
type can only represent numbers up to2^31 - 1
The number you entered overflows --
On the other hand, if you use the
Integer
type there is no overflow --