Exercise 65

Write a function in Python which takes as argument a tuple (a, b) composed of two integers and returns it the greatest common divisor GCD of a and b without using any predefined function in python.

Solution


def greatestCommonDiv(a,b):
# take d = a as initial value
d = a

# while d is not common divisor
while (a%d != 0 or b%d !=0):
# we decremente d
d = d - 1
return d

# testing function
print("The geatest common divisor of 8 and 12 is d = ",greatestCommonDiv(8,12))
# The output is : The geatest common divisor of 8 and 12 is d = 4
Younes Derfoufi
my-courses.net
One thought on “Solution Exercise 65: the greatest common divisor of two integers in python”

Leave a Reply