# Find two co-prime integers such that the first divides A and the second divides B

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 = 16Output:3 4

12 % 3 = 0

16 % 4 = 0

gcd(3, 4) = 1Input:A = 542, B = 762Output: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++

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the required numbers` `void` `findNumbers(` `int` `a, ` `int` `b)` `{` ` ` `// GCD of the given numbers` ` ` `int` `gcd = __gcd(a, b);` ` ` `// Printing the required numbers` ` ` `cout << (a / gcd) << ` `" "` `<< (b / gcd);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `a = 12, b = 16;` ` ` `findNumbers(a, b);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the approach` `import` `java.math.*;` `class` `GFG` `{` ` ` `public` `static` `int` `findGCD(` `int` `a, ` `int` `b)` ` ` `{` ` ` `if` `(b == ` `0` `)` ` ` `return` `a;` ` ` `else` ` ` `return` `findGCD(b, a % b);` ` ` `}` ` ` `// Function to find the required numbers` ` ` `static` `void` `findNumbers(` `int` `a, ` `int` `b)` ` ` `{` ` ` ` ` `// GCD of the given numbers` ` ` `int` `gcd = findGCD(a, b);` ` ` ` ` `// Printing the required numbers` ` ` `System.out.println((a / gcd) + ` `" "` `+` ` ` `(b / gcd));` ` ` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `a = ` `12` `, b = ` `16` `;` ` ` ` ` `findNumbers(a, b);` ` ` `}` `}` `// This code is contributed by Naman_Garg` |

## Python3

`# Python3 implementation of the approach` `# import gcd function from math module` `from` `math ` `import` `gcd` `# Function to find the required numbers` `def` `findNumbers(a, b) :` ` ` `# GCD of the given numbers` ` ` `__gcd ` `=` `gcd(a, b);` ` ` `# Printing the required numbers` ` ` `print` `((a ` `/` `/` `__gcd), (b ` `/` `/` `__gcd));` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `a ` `=` `12` `; b ` `=` `16` `;` ` ` `findNumbers(a, b);` `# This code is contributed by AnkitRai01` |

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG` `{` ` ` `public` `static` `int` `findGCD(` `int` `a, ` `int` `b)` ` ` `{` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `else` ` ` `return` `findGCD(b, a % b);` ` ` `}` ` ` `// Function to find the required numbers` ` ` `static` `void` `findNumbers(` `int` `a, ` `int` `b)` ` ` `{` ` ` ` ` `// GCD of the given numbers` ` ` `int` `gcd = findGCD(a, b);` ` ` ` ` `// Printing the required numbers` ` ` `Console.Write((a / gcd) + ` `" "` `+` ` ` `(b / gcd));` ` ` ` ` `}` ` ` `// Driver code` ` ` `static` `public` `void` `Main ()` ` ` `{` ` ` `int` `a = 12, b = 16;` ` ` ` ` `findNumbers(a, b);` ` ` `}` `}` `// This code is contributed by ajit` |

## Javascript

`<script>` `// Javascript implementation of the approach` `function` `findGCD(a, b)` `{` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `else` ` ` `return` `findGCD(b, a % b);` `}` `// Function to find the required numbers` `function` `findNumbers(a, b)` `{` ` ` ` ` `// GCD of the given numbers` ` ` `var` `gcd = findGCD(a, b);` ` ` ` ` `// Printing the required numbers` ` ` `document.write((a / gcd) + ` `" "` `+` ` ` `(b / gcd));` `}` `// Driver Code` `var` `a = 12, b = 16;` ` ` `findNumbers(a, b);` `// This code is contributed by Khushboogoyal499` ` ` `</script>` |

**Output:**

3 4

Time Complexity: O(log(min(a, b))), where a and b are the two parameters of __gcd

Auxiliary Space: O(log(min(a, b)))