From 0e19945c84e9aa05887df18d14dc3b543809b64c Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 22 Mar 2019 17:50:24 -0400 Subject: [PATCH] Fixed a bug in getDivisors where a number could be added twice --- Algorithms.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Algorithms.py b/Algorithms.py index f66ccb6..cd9313a 100644 --- a/Algorithms.py +++ b/Algorithms.py @@ -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()