Common Divisor Reduction Algorithm
Last Updated :
14 Sep, 2023
Given two integers x and y. In one step, we have to subtract the greatest common divisor of x and y from x and y each till x ≥ 1 and y ≥ 1. We have to find the minimum number of repetitions of this step.
Examples:
Input: x = 36, y = 16
Output: 4
Explanation: GCD of 36 and 16 is 4, so replace 36 and 16 with 32 and 12
GCD of 32 and 12 is 4, so replace 32 and 12 with 28 and 8
GCD of 28 and 8 is 4, so replace 28 and 8 with 24 and 4
GCD of 24 and 4 is 4, so replace 24 and 4 with 20 and 0
Now, as y < 1 so, process terminated. So steps repeated 4 times, so output will be 4.
Input: x = 60, y = 9
Output: 3
Recursive Approach: The approach is to calculate the greatest common divisor of the two input numbers, finds the maximum possible reduction in the two numbers, and calculate the amount of reduction needed. Then recursively calls itself with updated values of the two numbers after the reduction until the two numbers become less than 1.
Steps to implement the above approach:
- Define a recursive function “solve” which takes two integers a and b as input.
- Check if either a or b is zero, and return 0 if true.
- Check if a and b are equal, and return 1 if true.
- Find the greatest common divisor of a and b using the “__gcd” function.
- Swap a and b if a is greater than b.
- Calculate the values of x and y using the formulas: x = (b-a)/g and y = a/g, where g is the greatest common divisor of a and b.
- Find the maximum value of y/i * i for all factors i of x, and call this value mx.
- Calculate the reduction in a and b after performing the operation, and call it red. The formula for red is (y – mx) * g.
- If red is greater than or equal to a, return a/g.
- If red is zero, return a/g.
- Otherwise, recursively solve the problem on the new values of a and b after performing the operation. The formula for the return value is (y – mx) + solve(a – red, b – red).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return 1;
int g = __gcd(a, b);
if (a > b)
swap(a, b);
int x = (b - a) / g, y = a / g;
int mx = 0;
for ( int i = 2; i * i <= x; i++) {
if (x % i != 0)
continue ;
int j = x / i;
mx = max(mx, (y / i) * i);
mx = max(mx, (y / j) * j);
}
mx = max(mx, (y / x) * x);
int red = (y - mx) * g;
if (red >= a)
return a / g;
if (red == 0)
return a / g;
return (y - mx) + solve(a - red, b - red);
}
int main()
{
int x = 36, y = 16;
cout << solve(x, y);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int solve( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return 1 ;
int g = gcd(a, b);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
int x = (b - a) / g, y = a / g;
int mx = 0 ;
for ( int i = 2 ; i * i <= x; i++) {
if (x % i != 0 )
continue ;
int j = x / i;
mx = Math.max(mx, (y / i) * i);
mx = Math.max(mx, (y / j) * j);
}
mx = Math.max(mx, (y / x) * x);
int red = (y - mx) * g;
if (red >= a)
return a / g;
if (red == 0 )
return a / g;
return (y - mx) + solve(a - red, b - red);
}
public static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
public static void main(String[] args)
{
int x = 36 , y = 16 ;
System.out.println(solve(x, y));
}
}
|
Python3
import math
def solve(a, b):
if a = = 0 or b = = 0 :
return 0
if a = = b:
return 1
g = math.gcd(a, b)
if a > b:
a, b = b, a
x = (b - a) / / g
y = a / / g
mx = 0
for i in range ( 2 , int (math.sqrt(x)) + 1 ):
if x % i ! = 0 :
continue
j = x / / i
mx = max (mx, (y / / i) * i)
mx = max (mx, (y / / j) * j)
mx = max (mx, (y / / x) * x)
red = (y - mx) * g
if red > = a:
return a / / g
if red = = 0 :
return a / / g
return (y - mx) + solve(a - red, b - red)
if __name__ = = '__main__' :
x = 36
y = 16
print (solve(x, y))
|
C#
using System;
class GFG
{
public static int Solve( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return 1;
int g = GCD(a, b);
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
int x = (b - a) / g, y = a / g;
int mx = 0;
for ( int i = 2; i * i <= x; i++)
{
if (x % i != 0)
continue ;
int j = x / i;
mx = Math.Max(mx, (y / i) * i);
mx = Math.Max(mx, (y / j) * j);
}
mx = Math.Max(mx, (y / x) * x);
int red = (y - mx) * g;
if (red >= a)
return a / g;
if (red == 0)
return a / g;
return (y - mx) + Solve(a - red, b - red);
}
public static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static void Main( string [] args)
{
int x = 36, y = 16;
Console.WriteLine(Solve(x, y));
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function solve(a, b) {
if (a === 0 || b === 0) {
return 0;
}
if (a === b) {
return 1;
}
let g = gcd(a, b);
if (a > b) {
[a, b] = [b, a];
}
let x = (b - a) / g;
let y = a / g;
let mx = 0;
for (let i = 2; i * i <= x; i++) {
if (x % i !== 0) {
continue ;
}
let j = x / i;
mx = Math.max(mx, (y / i) * i);
mx = Math.max(mx, (y / j) * j);
}
mx = Math.max(mx, (y / x) * x);
let red = (y - mx) * g;
if (red >= a) {
return a / g;
}
if (red === 0) {
return a / g;
}
return (y - mx) + solve(a - red, b - red);
}
let x = 36, y = 16;
console.log(solve(x, y));
|
Time Complexity: O(sqrt(x)log(x))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...