Given an integer N. The task is to find a pair of co-prime divisors of N, greater than 1. If such divisors don’t exists then print ‘-1’.
Input: N = 45
Output: 3 5
Explanation: Since 3 and 5 are divisors of 45 and gcd( 3, 5 ) = 1 .
Hence, they satisfy the condition.
Input: N = 25
Explanation: No pair of divisors of 25 satisfy the condition such
that their gcd is 1.
- Iterate from x = 2 to sqrt(N), to find all divisors of N
- For any value x, check if it divides N
- If it divides, then keep dividing N by x as long as it is divisible.
- Now, check if N > 1, then the pair of divisors (x, N) will have
gcd(x, N) == 1, since all the factors of ‘x’ has been eliminated from N.
- Similarly, check for all values of x.
Below is the implementation of the above approach:
3 5 -1
Time Complexity : O(sqrt(N))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count of integers up to N which are non divisors and non coprime with N
- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if a number has an odd count of odd divisors and even count of even divisors
- Largest number less than or equal to N/2 which is coprime to N
- Partition first N natural number into two sets such that their sum is not coprime
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Finding a Non Transitive Coprime Triplet in a Range
- Composite XOR and Coprime AND
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Largest Coprime Set Between two integers
- Print all Coprime path of a Binary Tree
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all distinct Coprime sets possible from 1 to N
- Check if all the pairs of an array are coprime with each other
- Querying maximum number of divisors that a number in a given range has
- Total number of divisors for a given number
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.
Improved By : vishu2908