Largest number that divides x and is co-prime with y

12 Apr, 2021
Given two positive numbers x and y. Find the maximum valued integer a such that:  
a divides x i.e. x % a = 0a and y are co-prime i.e. gcd(a, y) = 1
a divides x i.e. x % a = 0
a and y are co-prime i.e. gcd(a, y) = 1
Examples :  
Input : x = 15
        y = 3 
Output : a = 5
Explanation: 5 is the max integer 
which satisfies both the conditions.
             15 % 5 =0
             gcd(5, 3) = 1
Hence, output is 5. 

Input : x = 14
        y = 28
Output : a = 1
Explanation: 14 % 1 =0
             gcd(1, 28) = 1
Hence, output is 1. 
 
Approach: Here, first we will remove the common factors of x and y from x by finding the greatest common divisor (gcd) of x and y and dividing x with that gcd. Mathematically:  
 x = x / gcd(x, y) —— STEP1 
Now, we repeat STEP1 till we get gcd(x, y) = 1. At last, we return a = x  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find the// Largest Coprime Divisor #include <bits/stdc++.h>using namespace std; // Recursive function to return gcd// of a and bint gcd(int a, int b){    // Everything divides 0    if (a == 0 || b == 0)        return 0;     // base case    if (a == b)        return a;     // a is greater    if (a > b)        return gcd(a - b, b);    return gcd(a, b - a);} // function to find largest// coprime divisorint cpFact(int x, int y){    while (gcd(x, y) != 1) {        x = x / gcd(x, y);    }    return x;} // divisor codeint main(){    int x = 15;    int y = 3;    cout << cpFact(x, y) << endl;    x = 14;    y = 28;    cout << cpFact(x, y) << endl;    x = 7;    y = 3;    cout << cpFact(x, y);    return 0;}
// java program to find the// Largest Coprime Divisorimport java.io.*; class GFG {    // Recursive function to return gcd    // of a and b    static int gcd(int a, int b)    {        // Everything divides 0        if (a == 0 || b == 0)            return 0;         // base case        if (a == b)            return a;         // a is greater        if (a > b)            return gcd(a - b, b);        return gcd(a, b - a);    }     // function to find largest    // coprime divisor    static int cpFact(int x, int y)    {        while (gcd(x, y) != 1) {            x = x / gcd(x, y);        }        return x;    }     // divisor code    public static void main(String[] args)    {        int x = 15;        int y = 3;        System.out.println(cpFact(x, y));        x = 14;        y = 28;        System.out.println(cpFact(x, y));        x = 7;        y = 3;        System.out.println(cpFact(x, y));    }} // This article is contributed by vt_m.
# Python3 code to find the# Largest Coprime Divisor # Recursive function to return# gcd of a and bdef gcd (a, b):         # Everything divides 0    if a == 0 or b == 0:        return 0         # base case    if a == b:        return a             # a is greater    if a > b:        return gcd(a - b, b)         return gcd(a, b - a) # function to find largest# coprime divisordef cpFact(x, y):    while gcd(x, y) != 1:        x = x / gcd(x, y)    return int(x)     # divisor codex = 15y = 3print(cpFact(x, y))x = 14y = 28print(cpFact(x, y))x = 7y = 3print(cpFact(x, y)) # This code is contributed by "Sharad_Bhardwaj".
// C# program to find the// Largest Coprime Divisorusing System; class GFG {     // Recursive function to return gcd    // of a and b    static int gcd(int a, int b)    {         // Everything divides 0        if (a == 0 || b == 0)            return 0;         // base case        if (a == b)            return a;         // a is greater        if (a > b)            return gcd(a - b, b);         return gcd(a, b - a);    }     // function to find largest    // coprime divisor    static int cpFact(int x, int y)    {        while (gcd(x, y) != 1) {            x = x / gcd(x, y);        }         return x;    }     // divisor code    public static void Main()    {         int x = 15;        int y = 3;        Console.WriteLine(cpFact(x, y));         x = 14;        y = 28;        Console.WriteLine(cpFact(x, y));         x = 7;        y = 3;        Console.WriteLine(cpFact(x, y));    }} // This code is contributed by vt_m.
<?php// PHP program to find the// Largest Coprime Divisor // Recursive function to// return gcd of a and bfunction gcd($a, $b){    // Everything divides 0    if ($a == 0 || $b == 0)        return 0;     // base case    if ($a == $b)        return $a;     // a is greater    if ($a > $b)        return gcd($a - $b, $b);    return gcd($a, $b - $a);} // function to find largest// coprime divisorfunction cpFact( $x, $y){    while (gcd($x, $y) != 1)    {        $x = $x / gcd($x, $y);    }    return $x;} // Driver Code$x = 15;$y = 3;echo cpFact($x, $y), "\n";$x = 14;$y = 28;echo cpFact($x, $y), "\n";$x = 7;$y = 3;echo cpFact($x, $y); // This code is contributed by aj_36?>
<script> // Javascript program to find the// Largest Coprime Divisor // Recursive function to// return gcd of a and bfunction gcd(a, b){    // Everything divides 0    if (a == 0 || b == 0)        return 0;     // base case    if (a == b)        return a;     // a is greater    if (a > b)        return gcd(a - b, b);    return gcd(a, b - a);} // function to find largest// coprime divisorfunction cpFact(x, y){    while (gcd(x, y) != 1)    {        x = x / gcd(x, y);    }    return x;} // Driver Codelet x = 15;let y = 3;document.write(cpFact(x, y) + "<br>");x = 14;y = 28;document.write(cpFact(x, y), "<br>");x = 7;y = 3;document.write(cpFact(x, y)); // This code is contributed by gfgking </script>
Output : 
5
1
7
 
jit_t
gfgking
divisibility
GCD-LCM
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end
Modulo 10^9+7 (1000000007)
The Knight's tour problem | Backtracking-1
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Program to find sum of elements in a given array