Question

In: Computer Science

Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a...

Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than m, then the gcd is the same as the gcd of m and n-m.

Pls endevour to use recursive approach to this

Solutions

Expert Solution

CODE:

def gcd(m,n):
# if m is Zero return n
 if(m==0):
  return n

# if n is zero retun m
 elif(n==0):
  return m 
  
 # if m is greater than or equal to n call gcd(n,m-n) 
 elif(m>=n):
  return gcd(n,m-n)

 # if n is greater than m then call gcd(m,n-m) 
 else:
  return gcd(m,n-m)

SAMPLE TEST CODE:

SAMPLE OUTPUT:

EXPLANATION:

Two Numbers taken as input from prompt and GCD of the Numbers is displayed on the next line.


Related Solutions

Write a C function gcd that returns the greatest common divisor of two integers. The greatest...
Write a C function gcd that returns the greatest common divisor of two integers. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers.
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1....
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1. Prove that there is a natural number k such that n divides ((m^k) − 1).
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf)...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers. The greatest common divisor of a and b is written as gcd (a, b), or sometimes simply as (a, b). For example, gcd (12, 18) = 6, gcd (−4, 14) = 2 and gcd (5, 0) = 5. Two numbers are called co-prime or relatively prime...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and uses your function to...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Python: Write a recursive function that accepts an integer argument, n. The function should display n...
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
Find the greatest common divisor d = gcd(527, 341). Show all calculation steps. We assume that...
Find the greatest common divisor d = gcd(527, 341). Show all calculation steps. We assume that the modulus is a positive integer. But the definition of the expression a mod n also makes perfect sense if n is negative. Determine the following: a. 7 mod 4 b. 7 mod -4 c. -7 mod 4 d. -7 mod -4
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT