mirror of
https://bitbucket.org/Mattrixwv/pyclasses.git
synced 2025-12-06 18:33:58 -05:00
Fixed a bug in getDivisors where a number could
be added twice
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user