Given two positive numbers x and y. Find the maximum valued integer a such that:
- 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
Algorithm:
Step 1: Define a function named gcd to find gcd of two numbers a and b.
Step 2: If a or b is equal to 0, return 0. If a is equal to b, return a.
Step 3: If a is greater than b, return gcd(a-b, b).
Step 4: If b is greater than a return gcd(a b-a).
Step 5: Define a function named cpFact to find the largest coprime divisor of two numbers x and y.
Step 6: While gcd(x, y) is not equal to 1, divide x by gcd(x,y).
Step 7: Return x as the largest coprime divisor.
below is the code implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
int cpFact( int x, int y)
{
while (gcd(x, y) != 1) {
x = x / gcd(x, y);
}
return x;
}
int 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
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static int cpFact( int x, int y)
{
while (gcd(x, y) != 1 ) {
x = x / gcd(x, y);
}
return x;
}
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));
}
}
|
Python3
def gcd (a, b):
if a = = 0 or b = = 0 :
return 0
if a = = b:
return a
if a > b:
return gcd(a - b, b)
return gcd(a, b - a)
def cpFact(x, y):
while gcd(x, y) ! = 1 :
x = x / gcd(x, y)
return int (x)
x = 15
y = 3
print (cpFact(x, y))
x = 14
y = 28
print (cpFact(x, y))
x = 7
y = 3
print (cpFact(x, y))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static int cpFact( int x, int y)
{
while (gcd(x, y) != 1) {
x = x / gcd(x, y);
}
return x;
}
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));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return gcd( $a - $b , $b );
return gcd( $a , $b - $a );
}
function cpFact( $x , $y )
{
while (gcd( $x , $y ) != 1)
{
$x = $x / gcd( $x , $y );
}
return $x ;
}
$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 );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
function cpFact(x, y)
{
while (gcd(x, y) != 1)
{
x = x / gcd(x, y);
}
return x;
}
let 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));
</script>
|
Output :
5
1
7
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Mar, 2023
Like Article
Save Article