Fixed a but where 2 was not being added to the prime numbers

This commit is contained in:
2019-02-04 20:34:11 -05:00
parent 6d7505ce9e
commit e4b59ed7c3

View File

@@ -14,10 +14,10 @@ def getPrimes(goalNumber: int):
#If the number is 0 or negative return an empty list #If the number is 0 or negative return an empty list
if(goalNumber <= 1): if(goalNumber <= 1):
return primes return primes
#Otherwise the number is at least 2, therefore 2 should be added to the list
#If the number is even 2 is a factor and all other factors will be odd else:
if((goalNumber %2) == 0):
primes.append(2) primes.append(2)
#We can now start at 3 and skip all even numbers, because they cannot be prime #We can now start at 3 and skip all even numbers, because they cannot be prime
for possiblePrime in range(3, goalNumber + 1, 2): #Need goalNumber + 1 to account for goalNumber being prime for possiblePrime in range(3, goalNumber + 1, 2): #Need goalNumber + 1 to account for goalNumber being prime
#Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor #Check all current primes, up to sqrt(possiblePrime), to see if there is a divisor