Open In App

Common Divisor Reduction Algorithm

Last Updated : 14 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to solve the problem
// "Common Divisor Reduction"
#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to
// solve the problem
int solve(int a, int b)
{
 
    // If either a or b is zero,
    // we can't perform the operation
    if (a == 0 || b == 0)
        return 0;
 
    // If a and b are equal, we can
    // only perform the operation once
    if (a == b)
        return 1;
 
    // Find the greatest common
    // divisor of a and b
    int g = __gcd(a, b);
 
    // Swap a and b if a is
    // greater than b
    if (a > b)
        swap(a, b);
 
    // Calculate the values of x and y
    int x = (b - a) / g, y = a / g;
 
    // Find the maximum value of y/i * i
    // for all factors i of x, and
    // call this value mx
    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);
 
    // Calculate the reduction in a and b
    // after performing the operation,
    // and call it red
    int red = (y - mx) * g;
 
    // If red is greater than or equal to
    // a, we can't perform the
    // operation anymore
    if (red >= a)
        return a / g;
 
    // If red is zero, we can only perform
    // the operation once
    if (red == 0)
        return a / g;
 
    // Otherwise, recursively solve the
    // problem on the new values of a and
    // b after performing the operation
    return (y - mx) + solve(a - red, b - red);
}
 
// Driver Code
int main()
{
 
    // Example usage
    int x = 36, y = 16;
 
    // Function Call
    cout << solve(x, y);
 
    return 0;
}


Java




// JAVA code to solve the problem
// "Common Divisor Reduction"
import java.util.*;
class GFG {
    public static int solve(int a, int b)
    {
 
        // If either a or b is zero,
        // we can't perform the operation
        if (a == 0 || b == 0)
            return 0;
 
        // If a and b are equal, we can
        // only perform the operation once
        if (a == b)
            return 1;
 
        // Find the greatest common
        // divisor of a and b
        int g = gcd(a, b);
 
        // Swap a and b if a is
        // greater than b
        if (a > b) {
            int temp = a;
            a = b;
            b = temp;
        }
 
        // Calculate the values of x and y
        int x = (b - a) / g, y = a / g;
 
        // Find the maximum value of y/i * i
        // for all factors i of x, and
        // call this value mx
        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);
 
        // Calculate the reduction in a and b
        // after performing the operation,
        // and call it red
        int red = (y - mx) * g;
 
        // If red is greater than or equal to
        // a, we can't perform the
        // operation anymore
        if (red >= a)
            return a / g;
 
        // If red is zero, we can only perform
        // the operation once
        if (red == 0)
            return a / g;
 
        // Otherwise, recursively solve the
        // problem on the new values of a and
        // b after performing the operation
        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));
    }
}
 
// This code is contributed by shivamgupta310570


Python3




import math
 
# Recursive function to solve the problem
 
 
def solve(a, b):
    # If either a or b is zero, we can't perform the operation
    if a == 0 or b == 0:
        return 0
 
    # If a and b are equal, we can only perform the operation once
    if a == b:
        return 1
 
    # Find the greatest common divisor of a and b
    g = math.gcd(a, b)
 
    # Swap a and b if a is greater than b
    if a > b:
        a, b = b, a
 
    # Calculate the values of x and y
    x = (b - a) // g
    y = a // g
 
    # Find the maximum value of y//i * i for all factors i of x, and call this value mx
    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)
 
    # Calculate the reduction in a and b after performing the operation, and call it red
    red = (y - mx) * g
 
    # If red is greater than or equal to a, we can't perform the operation anymore
    if red >= a:
        return a // g
 
    # If red is zero, we can only perform the operation once
    if red == 0:
        return a // g
 
    # Otherwise, recursively solve the problem on the new values of a and b after performing the operation
    return (y - mx) + solve(a - red, b - red)
 
 
# Driver code
if __name__ == '__main__':
    # Example usage
    x = 36
    y = 16
 
    # Function call
    print(solve(x, y))
 
 
# This code is contributed by shivamgupta310570


C#




using System;
 
class GFG
{
    public static int Solve(int a, int b)
    {
        // If either a or b is zero,
        // we can't perform the operation
        if (a == 0 || b == 0)
            return 0;
 
        // If a and b are equal, we can
        // only perform the operation once
        if (a == b)
            return 1;
 
        // Find the greatest common
        // divisor of a and b
        int g = GCD(a, b);
 
        // Swap a and b if a is
        // greater than b
        if (a > b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
 
        // Calculate the values of x and y
        int x = (b - a) / g, y = a / g;
 
        // Find the maximum value of y/i * i
        // for all factors i of x, and
        // call this value mx
        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);
 
        // Calculate the reduction in a and b
        // after performing the operation,
        // and call it red
        int red = (y - mx) * g;
 
        // If red is greater than or equal to
        // a, we can't perform the
        // operation anymore
        if (red >= a)
            return a / g;
 
        // If red is zero, we can only perform
        // the operation once
        if (red == 0)
            return a / g;
 
        // Otherwise, recursively solve the
        // problem on the new values of a and
        // b after performing the operation
        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 to calculate the gcd
function gcd(a, b) {
    if (b === 0) {
        return a;
    }
    return gcd(b, a % b);
}
 
// Recursive function to
// solve the problem
function solve(a, b) {
 
    // If either a or b is zero,
    // we can't perform the operation
    if (a === 0 || b === 0) {
        return 0;
    }
     
    // If a and b are equal, we can
    // only perform the operation once
    if (a === b) {
        return 1;
    }
     
    // Find the greatest common
    // divisor of a and b
    let g = gcd(a, b);
     
    // Swap a and b if a is
    // greater than b
    if (a > b) {
        [a, b] = [b, a];
    }
     
    // Calculate the values of x and y
    let x = (b - a) / g;
    let y = a / g;
     
    // Find the maximum value of y/i * i
    // for all factors i of x, and
    // call this value mx
    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);
     
    // Calculate the reduction in a and b
    // after performing the operation,
    // and call it red
    let red = (y - mx) * g;
     
    // If red is greater than or equal to
    // a, we can't perform the
    // operation anymore
    if (red >= a) {
        return a / g;
    }
    // If red is zero, we can only perform
    // the operation once
    if (red === 0) {
        return a / g;
    }
     
    // Otherwise, recursively solve the
    // problem on the new values of a and
    // b after performing the operation
    return (y - mx) + solve(a - red, b - red);
}
 
// Test case
let x = 36, y = 16;
console.log(solve(x, y));


Output

4






Time Complexity: O(sqrt(x)log(x))
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads