I am writing a program that computes n-n*(product(from i=1 to n) of (1-p_i)), which is rewritten in my code as: (1-product...)*n There are lots of primes involved here especially as n gets larger and larger. I do not want to use Atkin's method for generating them since my teacher and I will probably lose oversight as to what is going on, but would still like to optimize my algorithm for speed and accuracy (not sure if the latter can be better). Here is my code, written in Python: i = 0 j = 3 old = 1.0 primes = [2] while True: if (j>240000): break cont = True enumerator = 0 for e in primes: if j%e==0: cont = False if enumerator>=len(primes)/2 + 1: break enumerator += 1 if cont: primes.append(j) j+=2 #print primes while True: if (i>len(primes)-1): break old = old * (1 - 1.0/primes[i]) print old i+=1; primeslessthan = j-1 amta = 1-old print "The convergence: " + str(primeslessthan) print "All the way up to the number: " + str(amta) print "The amount of active inhibitors are: " + str((1-amta)*primeslessthan) I am open to multithreading, and to translating this to a different language like C++, but do not know what optimizations I could make there either.