Given a number N. The task is to find the number of co-prime pairs (a, b) from 1 to N such that their product(a*b) is equal to N.
Note: A pair(a, b) is said to be co-prime if gcd(a, b) = 1.
Input: N = 120 Output: No. of co-prime pairs = 3 (3, 40) (5, 24) (8, 15) Input: N= 250 Output: No. of co-prime pairs = 3 (2, 125)
Approach: Given that the elements in the pair should be co-prime to each other. Let a co prime pair be (a, b),
Given, a * b = N.
So the idea is to run a loop from 1 to and check whether i and (N/i) are coprime to each other or not and whether, i*(N/i) = N. If yes, then count such pairs.
Below is the implementation of the above approach:
The co- prime pairs are: (3, 40) (5, 24) (8, 15) Number of coprime pairs : 3
- Count number of ordered pairs with Even and Odd Product
- Largest number less than or equal to N/2 which is coprime to N
- Count ordered pairs with product less than N
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Minimum number of squares whose sum equals to given number n
- Composite XOR and Coprime AND
- Find smallest number n such that n XOR n+1 equals to given k.
- Finding a Non Transitive Coprime Triplet in a Range
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Count number of triplets with product equal to given number with duplicates allowed
- Sum and Product of digits in a number that divide the number
- Number of subarrays having product less than K
- Product of factors of number
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.