0

Recently interested in simple primality tests. I have the following two functions that return a list of all primes up to the given input. The first I made, the other is based off wikipedia's pseudocode for primality tests. I then slightly altered mine to be what I thought was the closest to wikipedia's.

When I time them (with 10000 as the input/limit), mine takes an order of magnitude longer than the other. I'm not quite sure why, as to me they're doing really similar things. Mine checks through a list of primes with "any", whereas wiki's checks through those same numbers but generates them with a while loop. What am I missing?

def my_primality(lim):                                                                                                                         
    count = 5                                                                                                                          
    slbprimes = [2,3];                                                                                                                 
    while count<lim:                                                                                                                   
        if count%3==0:                                                                                                                 
            pass                                                                                                                       
        elif any(count%i==0 for i in slbprimes if i**2<=count):                                                                        
            pass                                                                                                                       
        else:                                                                                                                          
            slbprimes.append(count)                                                                                                    
        count+=2                                                                                                                       
    return slbprimes 

def evenbetter(lim):                                                                                                                          
    count = 5                                                                                                                          
    ebprimes=[2,3];                                                                                                                    
    while count < lim:                                                                                                                 
        if count%3==0:                                                                                                                 
            count+=2                                                                                                                   
            continue                                                                                                                   
        i=5                                                                                                                            
        while i**2<=count:                                                                                                             
            if count%i==0 or count%(i+2)==0:                                                                                           
                count+=2                                                                                                               
                break                                                                                                                  
            i=i+6                                                                                                                      
        else:                                                                                                                          
            ebprimes.append(count)                                                                                                     
            count+=2                                                                                                                   
    return ebprimes 
dieggsy
  • 362
  • 1
  • 4
  • 13

1 Answers1

0

any will traverse the whole list in order to see if any of the elements are evaluated to true. The while loop breaks faster, because it takes into account the elements are in increasing order. You can play with takewhile from itertools and you should get similar run time as the while loop.

nikihub
  • 311
  • 1
  • 5
  • So you're saying `any` does _not_ stop at the first true statement? – dieggsy Jan 22 '16 at 17:45
  • 1
    It stops if it finds true statement, but if there a no true statements it will try all elements in the array and will not take advantage that it can break early when `if i**2<=count` fails. The while loop does take advantage of that. – nikihub Jan 22 '16 at 18:14