I have the following solution in Haskell to Problem 3:
isPrime :: Integer -> Bool
isPrime p = (divisors p) == [1, p]
divisors :: Integer -> [Integer]
divisors n = [d | d <- [1..n], n `mod` d == 0]
main = print (head (filter isPrime (filter ((==0) . (n `mod`)) [n-1,n-2..])))
where n = 600851475143
However, it takes more than the minute limit given by Project Euler. So how do I analyze the time complexity of my code to determine where I need to make changes?
Note: Please do not post alternative algorithms. I want to figure those out on my own. For now I just want to analyse the code I have and look for ways to improve it. Thanks!
Let’s start from the top.
For now, let’s assume that certain things are cheap: incrementing numbers is O(1), doing
modoperations is O(1), and comparisons with0are O(1). (These are false assumptions, but what the heck.) Thedivisorsfunction loops over all numbers from1ton, and does an O(1) operation on each number, so computing the complete output is O(n). Notice that here when we say O(n), n is the input number, not the size of the input! Since it takes m=log(n) bits to store n, this function takes O(2^m) time in the size of the input to produce a complete answer. I’ll use n and m consistently to mean the input number and input size below.In the worst case,
pis prime, which forcesdivisorsto produce its whole output. Comparison to a list of statically-known length is O(1), so this is dominated by the call todivisors. O(n), O(2^m)Your
mainfunction does a bunch of things at once, so let’s break down subexpressions a bit.This loops over a list, and does an O(1) operation on each element. This is O(m), where here m is the length of the input list.
Loops over a list, doing O(n) work on each element, where here n is the largest number in the list. If the list happens to be n elements long (as it is in your case), this means this is O(n*n) work, or O(2^m*2^m) = O(4^m) work (as above, this analysis is for the case where it produces its entire list).
Tiny bits of work. Let’s call it O(m) for the printing part.
Considering all the subexpressions above, the
filter isPrimebit is clearly the dominating factor. O(4^m), O(n^2)Now, there’s one final subtlety to consider: throughout the analysis above, I’ve consistently made the assumption that each function/subexpression was forced to produce its entire output. As we can see in
main, this probably isn’t true: we callhead, which only forces a little bit of the list. However, if the input number itself isn’t prime, we know for sure that we must look through at least half the list: there will certainly be no divisors betweenn/2andn. So, at best, we cut our work in half — which has no effect on the asymptotic cost.