Fixed a bug in getDivisors where a number could

be added twice
This commit is contained in:
2019-03-22 17:50:24 -04:00
parent b87d4e1e52
commit 0e19945c84

View File

@@ -1,7 +1,7 @@
#Python/myClasses/Algorithms.py
#Matthew Ellison
# Created: 01-27-19
#Modified: 02-09-19
#Modified: 03-22-19
#This is a file that contains a few algorithms that I have used several times
"""
Copyright (C) 2019 Matthew Ellison
@@ -150,13 +150,18 @@ def getDivisors(goalNumber: int) -> list:
#Start at 3 and loop through all numbers < (goalNumber / 2 ) looking for a number that divides it evenly
topPossibleDivisor = math.ceil(math.sqrt(goalNumber))
for possibleDivisor in range(2, topPossibleDivisor + 1): #Add one because we need <= goalNumber/2
possibleDivisor = 2
while(possibleDivisor <= topPossibleDivisor):
#If you find one add it and the number it creates to the list
if((goalNumber % possibleDivisor) == 0):
divisors.append(possibleDivisor)
#Account for the possibility sqrt(goalNumber) being a divisor
if(possibleDivisor != topPossibleDivisor):
divisors.append(goalNumber / possibleDivisor)
#Take care of a few occations where a number was added twice
if(divisors[len(divisors) - 1] == (possibleDivisor + 1)):
possibleDivisor += 1
possibleDivisor += 1
#Sort the list before returning for neatness
divisors.sort()