# Count all pairs of divisors of a number N whose sum is coprime with N

Given an integer N, the task is to count all pairs of divisors of N such that the sum of each pair is coprime with N.

Examples:

Input: N = 24
Output: 2
Explaination:
There are 2 pairs (1, 24) and (2, 3) whose sum is coprime with 24

Input: 105
Output: 4
Explaination:
There are 4 pairs (1, 105), (3, 35), (5, 21) and (7, 15) whose sum is coprime with 105

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

Approach:

To solve the problem mentioned above we can easily calculate the result by finding all divisors in √N complexity, and check for each pair, whether its sum is coprime with N or not.

Below is the implementation of the above approach:

## C++

 `// C++ program to count all pairs ` `// of divisors such that their sum ` `// is coprime with N ` `#include ` `using` `namespace` `std; ` ` `  `// Function to calculate GCD ` `int` `gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == 0) ` `        ``return` `a; ` ` `  `    ``return` `(gcd(b, a % b)); ` `} ` ` `  `// Function to count all valid pairs ` `int` `CountPairs(``int` `n) ` `{ ` ` `  `    ``// Initialize count ` `    ``int` `cnt = 0; ` ` `  `    ``for` `(``int` `i = 1; i * i <= n; i++) { ` `        ``if` `(n % i == 0) { ` `            ``int` `div1 = i; ` ` `  `            ``int` `div2 = n / i; ` ` `  `            ``int` `sum = div1 + div2; ` ` `  `            ``// Check if sum of pair ` `            ``// and n are coprime ` `            ``if` `(gcd(sum, n) == 1) ` ` `  `                ``cnt += 1; ` `        ``} ` `    ``} ` ` `  `    ``// Return the result ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `n = 24; ` ` `  `    ``cout << CountPairs(n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count all pairs ` `// of divisors such that their sum ` `// is coprime with N ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to calculate GCD ` `public` `static` `int` `gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == ``0``) ` `        ``return` `a; ` ` `  `    ``return` `(gcd(b, a % b)); ` `} ` ` `  `// Function to count all valid pairs ` `public` `static` `int` `CountPairs(``int` `n) ` `{ ` ` `  `    ``// Initialize count ` `    ``int` `cnt = ``0``; ` ` `  `    ``for``(``int` `i = ``1``; i * i <= n; i++)  ` `    ``{ ` `       ``if` `(n % i == ``0``) ` `       ``{ ` `           ``int` `div1 = i; ` `           ``int` `div2 = n / i; ` `           ``int` `sum = div1 + div2; ` `            `  `           ``// Check if sum of pair ` `           ``// and n are coprime ` `           ``if` `(gcd(sum, n) == ``1``) ` `               ``cnt += ``1``; ` `       ``} ` `    ``} ` ` `  `    ``// Return the result ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``24``; ` ` `  `    ``System.out.println(CountPairs(n)); ` `} ` `} ` ` `  `// This code is contributed by equbalzeeshan `

## Python3

 `# Python3 program to count all pairs ` `# of divisors such that their sum ` `# is coprime with N ` `import` `math as m ` ` `  ` `  `# Function to count all valid pairs ` `def` `CountPairs(n): ` `     `  `    ``# initialize count  ` `    ``cnt ``=` `0` `     `  `    ``i ``=` `1` `     `  `    ``while` `i ``*` `i <``=` `n : ` `         `  `        ``if``(n ``%` `i ``=``=` `0``): ` `             `  `            ``div1 ``=` `i ` `            ``div2 ``=` `n``/``/``i ` `             `  `            ``sum` `=` `div1 ``+` `div2; ` `             `  `            ``# Check if sum of pair ` `            ``# and n are coprime ` `            ``if``( m.gcd(``sum``, n) ``=``=` `1``): ` `                ``cnt ``+``=` `1` `         `  `        ``i ``+``=` `1` `     `  `    ``# Return the result ` `    ``return` `cnt ` `     `  ` `  `# Driver code  ` `n ``=` `24` ` `  `print``(CountPairs(n)) `

## C#

 `// C# program to count all pairs of ` `// divisors such that their sum ` `// is coprime with N ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to find gcd of a and b ` `    ``static` `int` `gcd(``int` `a, ``int` `b) ` `    ``{ ` `        ``if` `(b == 0) ` `            ``return` `a; ` `        ``return` `gcd(b, a % b); ` `    ``} ` ` `  `    ``// Function to count all valid pairs ` `    ``static` `int` `CountPairs(``int` `n) ` `    ``{ ` ` `  `        ``// Initialize count ` `        ``int` `cnt = 0; ` ` `  `        ``for` `(``int` `i = 1; i * i <= n; i++) { ` `            ``if` `(n % i == 0) { ` `                ``int` `div1 = i; ` ` `  `                ``int` `div2 = n / i; ` ` `  `                ``int` `sum = div1 + div2; ` ` `  `                ``// Check if sum of pair ` `                ``// and n are coprime ` `                ``if` `(gcd(sum, n) == 1) ` `                    ``cnt += 1; ` `            ``} ` `        ``} ` `        ``// Return the result ` `        ``return` `cnt; ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 24; ` ` `  `        ``Console.WriteLine(CountPairs(n)); ` `    ``} ` `} `

Output:

```2
```

Time Complexity: O(√N * log(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 : equbalzeeshan

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.