# Coprime divisors of a number

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’.

Examples:

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
Output: -1
Explanation: No pair of divisors of 25 satisfy the condition such
that their gcd is 1.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Iterate from x = 2 to sqrt(N), to find all divisors of N
2. For any value x, check if it divides N
3. If it divides, then keep dividing N by x as long as it is divisible.
4. 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.
5. Similarly, check for all values of x.

Below is the implementation of the above approach:

## C++

 `// C++ program to find two coprime ` `// divisors of a given number ` `// such that both are greater ` `// than 1 ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function which finds the ` `// required pair of divisors ` `// of N ` `void` `findCoprimePair(``int` `N) ` `{ ` `    ``// We iterate upto sqrt(N) ` `    ``// as we can find all the ` `    ``// divisors of N in this ` `    ``// time ` `    ``for` `(``int` `x = 2; x <= ``sqrt``(N); x++) { ` `        ``if` `(N % x == 0) { ` `            ``// If x is a divisor of N ` `            ``// keep dividing as long ` `            ``// as possible ` `            ``while` `(N % x == 0) { ` `                ``N /= x; ` `            ``} ` `            ``if` `(N > 1) { ` `                ``// We have found a ` `                ``// required pair ` `                ``cout << x << ``" "` `                     ``<< N << endl; ` `                ``return``; ` `            ``} ` `        ``} ` `    ``} ` `    ``// No such pair of divisors ` `    ``// of N was found, hence ` `    ``// print -1 ` `    ``cout << -1 << endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Sample example 1 ` `    ``int` `N = 45; ` `    ``findCoprimePair(N); ` ` `  `    ``// Sample example 2 ` `    ``N = 25; ` `    ``findCoprimePair(N); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 program to find two coprime  ` `# divisors of a given number such that ` `# both are greater than 1  ` `import` `math ` ` `  `# Function which finds the  ` `# required pair of divisors  ` `# of N  ` `def` `findCoprimePair(N): ` `     `  `    ``# We iterate upto sqrt(N) ` `    ``# as we can find all the ` `    ``# divisors of N in this ` `    ``# time ` `    ``for` `x ``in` `range``(``2``, ``int``(math.sqrt(N)) ``+` `1``): ` `        ``if` `(N ``%` `x ``=``=` `0``): ` `             `  `            ``# If x is a divisor of N ` `            ``# keep dividing as long ` `            ``# as possible ` `            ``while` `(N ``%` `x ``=``=` `0``): ` `                ``N ``/``/``=` `x ` ` `  `            ``if` `(N > ``1``): ` ` `  `                ``# We have found a ` `                ``# required pair ` `                ``print``(x, N) ` `                ``return``; ` ` `  `    ``# No such pair of divisors ` `    ``# of N was found, hence ` `    ``# print -1 ` `    ``print``(``"-1"``)  ` ` `  `# Driver Code ` ` `  `# Sample example 1 ` `N ``=` `45` `findCoprimePair(N) ` ` `  `# Sample example 2 ` `N ``=` `25` `findCoprimePair(N) ` ` `  `# This code is contributed by Vishal Maurya. `

Output:

```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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.