Advertisement
Guest User

Prime Checker Sum

a guest
Sep 18th, 2020
2,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  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_file.txt') as the_file: pi = the_file.read()         # This file must start with 14159 and not have the "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 = 1299721 # The 100,001st prime
  34. list_of_primes = prime_list(biggest_prime)
  35.  
  36. #-----------------------------------------------------------------------
  37.  
  38. for i in range(1, 15000):
  39.     print(i)
  40.  
  41.     nth_prime = list_of_primes[i - 1]
  42.     twice_nth_prime = nth_prime * 2
  43.  
  44.     s = str(twice_nth_prime)
  45.     digits = pi.split(s)[0] + s
  46.     end_of = len(digits)
  47.     digit_sum = 0
  48.  
  49.     for digit in digits:
  50.         digit_sum += int(digit)
  51.  
  52.     if digit_sum % nth_prime == 0:
  53.         print(twice_nth_prime)
  54.         raw_input('We have a winner!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement