Advertisement
HasteBin0

Python Primes 6n±1 Program

Aug 6th, 2017
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from math import sqrt, floor
  3.  
  4. # How many primes are 6n +/- 1?
  5.  
  6. total: int = 0
  7. prime: int = 0
  8.  
  9. def is_prime(x: int) :
  10.     for y in range(2, floor(sqrt(x) + 1)) :
  11.         if x % y == 0 :
  12.             return False
  13.     return True
  14.  
  15. with open('prime_list.txt', 'w') as of :
  16.     for x in range(1, 1000000) :
  17.         for y in -1, 1 :
  18.             z = 6 * x + y
  19.             z1 = is_prime(z)
  20.             total += 1
  21.             prime += 1 if z1 else 0
  22.             #
  23.             print('6({:>10}){:+}={:>11} {} {:>4.15}%'.format(
  24.                 x,
  25.                 y,
  26.                 z,
  27.                 ('T' if z1 else 'F'),
  28.                 (prime * 100 / total)
  29.             ), file=of)
  30.  
  31. input('Done.')
  32. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement