Skip to main content
2 of 3
deleted 20 characters in body
flakes
  • 1.9k
  • 1
  • 16
  • 28

This got around 11000 points using a limit of 13.5 million, in roughly 2 seconds

Basically my strategy is to create a lookup list out made of bools, where at the end of each cycle in my main loop, the next true value with a higher index than the current indexed value is guaranteed to be prime.

The first thing i do when I evaluate a new prime, is to eliminate its multiples from the rest of the list.

After that I get the reverse of my current prime value and perform:

  • 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:
                 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)
    
flakes
  • 1.9k
  • 1
  • 16
  • 28