Prime numbers/Prime number generators
This code is used on Python3.
from tqdm import tqdm
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
f = 5
while f <= r:
if n%f == 0: return False
if n%(f+2) == 0: return False
f +=6
return True
def prime_gen(amt):
n = 1
cur = 0
while True:
if not is_prime(n):
n += 1
else:
yield n
n += 1
cur += 1
if cur == amt:
break
def prime_filegen(pn):
with open('primes_{}.txt'.format(str(pn)),'w') as file:
for i in tqdm(prime_gen(pn), total = pn):
f = file.write('# '+str(i)+'\n')
The purpose of tqdm is to provide a nice progressbar for progress. It can be removed by replacing the second-to-last with
for i in prime_gen(pn):
and removing the first line.