Link to home
Start Free TrialLog in
Avatar of Maria Qureshi
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.
Avatar of gelonida
gelonida
Flag of France image

you just want the answer or you want to learn by sending us what you know and getting tips from us to progress?


Up to you.
Euclid.  See  https://en.wikipedia.org/wiki/Euclidean_algorithm

That should give you enough information to get going.
@Sidra:

depending on your answer I'll reply with different information
Avatar of Maria Qureshi
Maria Qureshi

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
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:
result = gcd(a, b)
print(result)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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