mirror of
https://bitbucket.org/Mattrixwv/pyclasses.git
synced 2025-12-06 18:33:58 -05:00
Neatened primeGenerator
This commit is contained in:
@@ -22,6 +22,7 @@ Copyright (C) 2019 Matthew Ellison
|
||||
|
||||
|
||||
import math
|
||||
import itertools
|
||||
|
||||
|
||||
#Generate an infinite sequence of prime numbers using the Sieve of Eratosthenes
|
||||
@@ -33,9 +34,8 @@ def primeGenerator():
|
||||
#Map composite integers to primes witnessing their compositeness
|
||||
dict = {}
|
||||
|
||||
#Start checking for primes with the number 3
|
||||
possiblePrime = 3
|
||||
while True:
|
||||
#Start checking for primes with the number 3 and skip all even numbers
|
||||
for possiblePrime in itertools.count(3, 2):
|
||||
#If q is not in the dictionary it is a new prime number
|
||||
#Return it and mark it's next multiple
|
||||
if possiblePrime not in dict:
|
||||
@@ -49,9 +49,6 @@ def primeGenerator():
|
||||
#We no longer need this, free the memory
|
||||
del dict[possiblePrime]
|
||||
|
||||
#Skip all multiples of 2
|
||||
possiblePrime += 2
|
||||
|
||||
#Generate an inifinite sequence of fibonacci numbers
|
||||
def fibGenerator():
|
||||
#Set this so the first returned number is 1 and the second is also 1
|
||||
|
||||
Reference in New Issue
Block a user