Case1 If the reversed value is lower than the non reversed, I check to see if it is also a prime using my lookup list. If its also a prime value I only add the original value.
Case2 if reversed value is higher than my overall limit I perform a simple check on it using a common prime evaluating function. If it is prime I add the non reversed prime
Case3 If the reversed value higher than the non reversed prime and lower than the limit I will ignore it seeing as it will be found again under Case1
from time import time def is_prime(n): for i in xrange(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return True def DoMath(limit): start = time() lkup = [True,True,True] +[ bool(ii%2) for ii in xrange(3,limit)] with open("text.txt", 'w') as file: index = 3 r_index = 0 str_index = '' while index < limit: if lkup[index]: for ii in xrange(index*2, limit, index): lkup[ii] = False str_index = str(index) r_index = int(str_index[::-1]) if r_index >= limit and is_prime(r_index): file.write(str_index+'\n') elif r_index < index and lkup[r_index]: file.write(str_index+'\n') index += 1 try: while not lkup[index]: index += 1 except IndexError: break end=time() print("Time Taken : ",end-start)