Find two co-prime integers such that the first divides A and the second divides B
Last Updated :
04 Oct, 2023
Given two integers A and B, the task is to find two co-prime numbers C1 and C2 such that C1 divides A and C2 divides B.
Examples:
Input: A = 12, B = 16
Output: 3 4
12 % 3 = 0
16 % 4 = 0
gcd(3, 4) = 1
Input: A = 542, B = 762
Output: 271 381
Naive approach: A simple solution is to store all of the divisors of A and B then iterate over all the divisors of A and B pairwise to find the pair of elements which are co-prime.
Efficient approach: If an integer d divides gcd(a, b) then gcd(a / d, b / d) = gcd(a, b) / d. More formally, if num = gcd(a, b) then gcd(a / num, b / num) = 1 i.e. (a / num) and (b / num) are relatively co-prime.
So in order to find the required numbers, find gcd(a, b) and store it in a variable gcd. Now the required numbers will be (a / gcd) and (b / gcd).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNumbers( int a, int b)
{
int gcd = __gcd(a, b);
cout << (a / gcd) << " " << (b / gcd);
}
int main()
{
int a = 12, b = 16;
findNumbers(a, b);
return 0;
}
|
Java
import java.math.*;
class GFG
{
public static int findGCD( int a, int b)
{
if (b == 0 )
return a;
else
return findGCD(b, a % b);
}
static void findNumbers( int a, int b)
{
int gcd = findGCD(a, b);
System.out.println((a / gcd) + " " +
(b / gcd));
}
public static void main(String[] args)
{
int a = 12 , b = 16 ;
findNumbers(a, b);
}
}
|
Python3
from math import gcd
def findNumbers(a, b) :
__gcd = gcd(a, b);
print ((a / / __gcd), (b / / __gcd));
if __name__ = = "__main__" :
a = 12 ; b = 16 ;
findNumbers(a, b);
|
C#
using System;
class GFG
{
public static int findGCD( int a, int b)
{
if (b == 0)
return a;
else
return findGCD(b, a % b);
}
static void findNumbers( int a, int b)
{
int gcd = findGCD(a, b);
Console.Write((a / gcd) + " " +
(b / gcd));
}
static public void Main ()
{
int a = 12, b = 16;
findNumbers(a, b);
}
}
|
Javascript
<script>
function findGCD(a, b)
{
if (b == 0)
return a;
else
return findGCD(b, a % b);
}
function findNumbers(a, b)
{
var gcd = findGCD(a, b);
document.write((a / gcd) + " " +
(b / gcd));
}
var a = 12, b = 16;
findNumbers(a, b);
</script>
|
Time Complexity: O(log(min(a, b))), where a and b are the two parameters of __gcd
Auxiliary Space: O(log(min(a, b)))
Approach: Use the Euclidean algorithm, which uses recursion to find the GCD of two numbers.
Algorithm steps:
- Set two variables, x and y, to the values of a and b respectively.
- While y is not equal to 0, do the following:
a. Set a to the value of b.
b. Set b to the remainder of x divided by y.
c. Set x to the value of y.
d. Set y to the remainder of a divided by b.
- Return the value of x, which is the GCD of a and b.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
void findNumbers( int a, int b) {
int gcd_ab = gcd(a, b);
int num1 = a / gcd_ab;
int num2 = b / gcd_ab;
cout << num1 << " " << num2;
}
int main() {
int a = 12, b = 16;
findNumbers(a, b);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int gcd( int a, int b) {
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
public static void findNumbers( int a, int b) {
int gcd_ab = gcd(a, b);
int num1 = a / gcd_ab;
int num2 = b / gcd_ab;
System.out.println(num1 + " " + num2);
}
public static void main(String[] args) {
int a = 12 , b = 16 ;
findNumbers(a, b);
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def findNumbers(a, b):
gcd_ab = gcd(a, b)
num1 = a / / gcd_ab
num2 = b / / gcd_ab
print (num1, num2)
def main():
a = 12
b = 16
findNumbers(a, b)
if __name__ = = "__main__" :
main()
|
C#
using System;
namespace GCDExample
{
class Program
{
static int GCD( int a, int b)
{
if (b == 0)
{
return a;
}
return GCD(b, a % b);
}
static void FindNumbers( int a, int b)
{
int gcd_ab = GCD(a, b);
int num1 = a / gcd_ab;
int num2 = b / gcd_ab;
Console.WriteLine(num1 + " " + num2);
}
static void Main( string [] args)
{
int a = 12, b = 16;
FindNumbers(a, b);
}
}
}
|
Javascript
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function findNumbers(a, b) {
const gcd_ab = gcd(a, b);
const num1 = a / gcd_ab;
const num2 = b / gcd_ab;
console.log(num1 + " " + num2);
}
const a = 12, b = 16;
findNumbers(a, b);
|
Time Complexity: O(n), where n is the maximum of the given two numbers a and b, because we are iterating over a range of numbers from 1 to the maximum of the two numbers.
Auxiliary Space: O(1), because we are not using any additional space to store variables or data structures in the program.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...