Guest User

Prime (#) Double Location Search

a guest
Oct 15th, 2020
234
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. # Use this code if you wish to generate Pi rather than opening it through a file
  3. # You will need mpmath to do this
  4. # pip install mpmath
  5.  
  6. try:
  7.     from sympy.mpmath import mp
  8. except ImportError:
  9.     from mpmath import mp
  10.  
  11. print('Generating 99,999 digits of Pi...\n')
  12.  
  13. pi = ''
  14. mp.dps = 100000
  15. pi = str(mp.pi)[2:][:-3] + '541'        # Remove 2 characters '3.' from the front and remove 3 incorrect digits from the end
  16. """
  17.  
  18. pi = ''
  19.  
  20. with open('/path/to/pi.txt') as the_file: pi = the_file.read()      # This file should start without "3." in the front
  21.  
  22. #-----------------------------------------------------------------------
  23.  
  24. def prime_list(n):
  25.     sieve = [True] * n
  26.     for i in xrange(3, int(n ** 0.5) + 1, 2):
  27.         if sieve[i]:
  28.             sieve[i * i::2 * i]=[False]*((n - i * i - 1) / (2 * i) + 1)
  29.     return [2] + [i for i in xrange(3, n, 2) if sieve[i]]
  30.  
  31. print('Generating 100,000 prime numbers...\n')
  32.  
  33. biggest_prime = 1299709 # Prime #100000
  34. list_of_primes = prime_list(biggest_prime)
  35.  
  36. #-----------------------------------------------------------------------
  37.  
  38. for i in range(1, 100000):
  39.     nth_prime = list_of_primes[i - 1]
  40.     doubled = i * 2
  41.  
  42.     s = str(nth_prime)
  43.     digits = pi.split(s)[0] + s
  44.     end_of = len(digits)
  45.  
  46.     if doubled == end_of:
  47.         print(str(nth_prime) + ' is prime #' + str(i))
  48.         print(str(nth_prime) + ' first appears in Pi at the end of ' + str(end_of) + ' digits\n')
  49.  
  50. """
  51. Pi
  52. 71 (prime #20) first appears in Pi at the end of 40 digits
  53. 359 (prime #72) first appears in Pi at the end of 144 digits
  54.  
  55. Phi
  56. 43 (prime #14) first appears in Phi at the end of 28 digits
  57.  
  58. e
  59. 85781 (prime #8346) first appears in e at the end of 16692 digits
  60.  
  61. 2Pi
  62. 5 (prime #3) first appears in 2Pi at the end of 6 digits
  63. """
RAW Paste Data