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.
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:
- Largest Coprime Set Between two integers
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of integers up to N which are non divisors and non coprime with N
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Composite XOR and Coprime AND
- Find integers that divides maximum number of elements of the array
- Finding a Non Transitive Coprime Triplet in a Range
- Largest number less than or equal to N/2 which is coprime to N
- Print all Coprime path of a Binary Tree
- Print all distinct Coprime sets possible from 1 to N
- Coprime divisors of a number
- Count all pairs of divisors of a number N whose sum is coprime with N
- Check if all the pairs of an array are coprime with each other
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Find the first N integers such that the sum of their digits is equal to 10
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Find a distinct pair (x, y) in given range such that x divides y
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.