Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Bad Algorithm solution in Uncategorized category for The Greatest Common Divisor by MackM
def greatest_common_divisor(*args):
Factors = Factor(args[0])
for Number in args:
Factors = Factors.intersection(Factor(Number))
return max(Factors)
def Factor(Number):
'''
Returns all factors of a number as a set of integers
'''
Factors = set((1,Number))
Divisor = 2
Stop = int(Number ** 0.5) + 1
while Divisor < Stop:
if Number % Divisor == 0:
Factors.update((Divisor,int(Number/Divisor)))
Divisor += 1
return Factors
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert greatest_common_divisor(6, 4) == 2, "Simple"
assert greatest_common_divisor(2, 4, 8) == 2, "Three arguments"
assert greatest_common_divisor(2, 3, 5, 7, 11) == 1, "Prime numbers"
assert greatest_common_divisor(3, 9, 3, 9) == 3, "Repeating arguments"
Aug. 27, 2014
Comments: