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 math
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
|
||||||
#Generate an infinite sequence of prime numbers using the Sieve of Eratosthenes
|
#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
|
#Map composite integers to primes witnessing their compositeness
|
||||||
dict = {}
|
dict = {}
|
||||||
|
|
||||||
#Start checking for primes with the number 3
|
#Start checking for primes with the number 3 and skip all even numbers
|
||||||
possiblePrime = 3
|
for possiblePrime in itertools.count(3, 2):
|
||||||
while True:
|
|
||||||
#If q is not in the dictionary it is a new prime number
|
#If q is not in the dictionary it is a new prime number
|
||||||
#Return it and mark it's next multiple
|
#Return it and mark it's next multiple
|
||||||
if possiblePrime not in dict:
|
if possiblePrime not in dict:
|
||||||
@@ -49,9 +49,6 @@ def primeGenerator():
|
|||||||
#We no longer need this, free the memory
|
#We no longer need this, free the memory
|
||||||
del dict[possiblePrime]
|
del dict[possiblePrime]
|
||||||
|
|
||||||
#Skip all multiples of 2
|
|
||||||
possiblePrime += 2
|
|
||||||
|
|
||||||
#Generate an inifinite sequence of fibonacci numbers
|
#Generate an inifinite sequence of fibonacci numbers
|
||||||
def fibGenerator():
|
def fibGenerator():
|
||||||
#Set this so the first returned number is 1 and the second is also 1
|
#Set this so the first returned number is 1 and the second is also 1
|
||||||
|
|||||||
Reference in New Issue
Block a user