Maria Qureshi
asked on
Greatest Common Divisor (GCD)
Write a function called my_gcd() that takes parameters a and b and returns their greatest common divisor. Create your own implementation of the algorithm and test your code by comparing the output of your function to Python's gcd() function from math package.Test the code with the following inputs: (39, 91), (20, 30) and (40,40). Explain the logic of your function.
Euclid. See https://en.wikipedia.org/wiki/Euclidean_algorithm
That should give you enough information to get going.
That should give you enough information to get going.
@Sidra:
depending on your answer I'll reply with different information
depending on your answer I'll reply with different information
ASKER
a = 39
b=91
def gcd(a,b):
if b > a:
return gcd(b,a)
r = a%b
if r == 0:
return b
return gcd(r,b)
I don't get a return value
b=91
def gcd(a,b):
if b > a:
return gcd(b,a)
r = a%b
if r == 0:
return b
return gcd(r,b)
I don't get a return value
the reason you get no result is, that you just declared the variables and the function.
you did not call it.
just add et the end of your script:
you did not call it.
just add et the end of your script:
result = gcd(a, b)
print(result)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I have recommended this question be closed as follows:
Accept: gelonida (https:#a42330357)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer
I have recommended this question be closed as follows:
Accept: gelonida (https:#a42330357)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
suhasbharadwaj
Experts-Exchange Cleanup Volunteer
Up to you.