def slowPrimeDigits(maxValue): """ Very slow way to make a list of primes Start a list, then remove each number that is a prime times n Slow as removing an item from a list means moving every element above it """ # make an array to hold each number up to maxValue primes = [] # fill the array for i in range(maxValue): primes.append(i) # zero and one are not prime primes.remove(0) primes.remove(1) # now apply Sieve of Eratosthenes (google it...) for i in primes: # make a list of all factors of i (next prime) factors = range(i, maxValue, i) # first factor is prime (it stays) the rest are removed for f in factors[1:]: # check if f is still in primes if f in primes: # if so remove from primes.remove(f) # output return primes # set maxmimum value # for the "slow" code only do up to 10k max10k = 10000 sumOfPrimes10k = 5736396 # for fast code can do up to 1M max1M = 1000000 sumOfPrimes1M = 37550402023 # timeit runs repeated loops on a line to check how fast it is # check output is what we expect... # test new code with import numpy if(numpy.sum(slowPrimeDigits(max10k)) != sumOfPrimes10k): print('\nCode failed') else: print('\nCode worked') # with ipython use "magic" timeit command print("\nTiming Test with \ntimeit if(numpy.sum(slowPrimeDigits(max10k)) != sumOfPrimes10k): print('Code failed')") import profile print("\nProfile the code:\n") profile.run('out = slowPrimeDigits({})'.format(max10k))