Question

In python, create a well documented program that uses the Euclidean Algorithm to compute the greatest...

In python, create a well documented program that uses the Euclidean Algorithm to compute the greatest common divisor of two integers a and b.

Homework Answers

Answer #1
def gcd(n1, n2):
    while n2 != 0:  # as long as second number is not 0
        temp = n1   # back up n1 in temp
        n1 = n2     # set n1 to n2
        n2 = temp % n2  # set remainder of n1 when divided with n2 in n2
    return n1   # return final value of n2


n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))
print("GCD({}, {}) is {}".format(n1, n2, gcd(n1, n2)))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the extended Euclidean algorithm, compute the greatest common divisor of 1819 and 3587.
Using the extended Euclidean algorithm, compute the greatest common divisor of 1819 and 3587.
Using the Euclidean Algorithm, find the Greatest Common Divisor and Bezout Coefficients of the following pair...
Using the Euclidean Algorithm, find the Greatest Common Divisor and Bezout Coefficients of the following pair of integers: (2002, 2339)
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0. The greatest common divisor of 210...
Find the greatest common factor applying Euclidean Algorithm: 29448, 59220
Find the greatest common factor applying Euclidean Algorithm: 29448, 59220
(Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that...
(Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a JAVA program that returns the greatest common divisor of two integers.
Write a program called gcd that accepts two positive integers in the range of [32,256] as...
Write a program called gcd that accepts two positive integers in the range of [32,256] as parameters and prints the greatest common divisor (GCD) of the two numbers. The GCD of two integers a and b is the largest integer that is a factor of both a and b. One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following: GCD (a, b) _ GCD (b, a % b) GCD (a, 0)...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers a and b, together with a solution (u, v) in integers to the equation au + bv = gcd(a, b). 1. Set u = 1, g = a, x = 0, and y = b 2. If y = 0, set v = (g − au)/b and return the values (g, u, v) 3. Divide g by y with remainder, g = qy +...
Using Windows 32 framework , write an assembly language program; Write a recursive procedure to find...
Using Windows 32 framework , write an assembly language program; Write a recursive procedure to find the greatest common divisor of two non negative numbers. You should use Euclidean algorithm and this is typically discussed in CSC 230. Your procedure: ❼ Needs to follow cdecl protocol. ❼ Needs to take two parameters. ❼ Should return -1, if the parameters are negative. ❼ Should return gcd, if the parameters are non negative. Your main procedure should do the followings. ❼ Read...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
Two numbers are relatively prime if their greatest common divisor is 1. Show that if a...
Two numbers are relatively prime if their greatest common divisor is 1. Show that if a and b are relatively prime, then there exist integers m and n such that am+bn = 1. (proof by induction preferred)