Guest User

Reverse Check

a guest
Sep 26th, 2020
339
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pi = ''
  2.  
  3. with open('/path/to/pi.txt') as the_file: pi = the_file.read()
  4.  
  5. pi = pi[2:]     # If your Pi file starts with "3."
  6.  
  7. pi_len = len(pi)
  8.  
  9. #-----------------------------------------------------------------------
  10.  
  11. def prime_list(n):
  12.     sieve = [True] * n
  13.     for i in xrange(3, int(n ** 0.5) + 1, 2):
  14.         if sieve[i]:
  15.             sieve[i * i::2 * i]=[False]*((n - i * i - 1) / (2 * i) + 1)
  16.     return [2] + [i for i in xrange(3, n, 2) if sieve[i]]
  17.  
  18. print('Generating prime numbers...\n')
  19.  
  20. biggest_prime = 373587883 # Prime #20,000,000
  21. list_of_primes = prime_list(biggest_prime)
  22.  
  23. #-----------------------------------------------------------------------
  24.  
  25. for i, prime in enumerate(list_of_primes):
  26.     if prime > pi_len: exit()
  27.  
  28.     num = i + 1
  29.  
  30.     if num % 10 != 0: continue
  31.  
  32.     amount = 10 ** len(str(num))
  33.     remainder = amount - num
  34.     back_r = str(remainder)[::-1]
  35.     digits = pi[:prime]
  36.  
  37.     if digits.endswith(back_r):
  38.         print('Prime: #' + str(num) + ' is ' + str(prime))
  39.         print('Amount: ' + str(amount))
  40.         print('Calculation: ' + str(amount) + ' - ' + str(num) + ' = ' + str(remainder))
  41.         print('Number we are looking for backwards: ' + str(back_r) + '\n\n')
  42.         #raw_input()
RAW Paste Data