# Count pairs of numbers from 1 to N with Product divisible by their Sum

Given a number . The task is to count pairs (x, y) such that x*y is divisible by (x+y) and the condition **1 <= x < y < N** holds true.

**Examples**:

Input: N = 6Output: 1Explanation: The only pair is (3, 6) which satisfies all of the given condition, 3<6 and 18%9=0.Input: N = 15Output: 4

The basic approach is to iterate using two loops carefully maintaining the given condition **1 <= x < y < N** and generate all possible valid pairs and count such pairs for which the product of their values is divisible by sum.

Below is the implementation of the above approach:

## C++

`// C++ program to count pairs of numbers ` `// from 1 to N with Product divisible ` `// by their Sum ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count pairs ` `int` `countPairs(` `int` `n) ` `{ ` ` ` `// variable to store count ` ` ` `int` `count = 0; ` ` ` ` ` `// Generate all possible pairs such that ` ` ` `// 1 <= x < y < n ` ` ` `for` `(` `int` `x = 1; x < n; x++) { ` ` ` `for` `(` `int` `y = x + 1; y <= n; y++) { ` ` ` `if` `((y * x) % (y + x) == 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` ` ` `cout << countPairs(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count pairs of numbers ` `// from 1 to N with Product divisible ` `// by their Sum ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` ` ` `// Function to count pairs ` `static` `int` `countPairs(` `int` `n) ` `{ ` ` ` `// variable to store count ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Generate all possible pairs such that ` ` ` `// 1 <= x < y < n ` ` ` `for` `(` `int` `x = ` `1` `; x < n; x++) { ` ` ` `for` `(` `int` `y = x + ` `1` `; y <= n; y++) { ` ` ` `if` `((y * x) % (y + x) == ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `15` `; ` ` ` ` ` `System.out.println(countPairs(n)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count pairs of numbers ` `# from 1 to N with Product divisible ` `# by their Sum ` ` ` `# Function to count pairs ` `def` `countPairs(n): ` ` ` ` ` `# variable to store count ` ` ` `count ` `=` `0` ` ` ` ` `# Generate all possible pairs such that ` ` ` `# 1 <= x < y < n ` ` ` `for` `x ` `in` `range` `(` `1` `, n): ` ` ` `for` `y ` `in` `range` `(x ` `+` `1` `, n ` `+` `1` `): ` ` ` `if` `((y ` `*` `x) ` `%` `(y ` `+` `x) ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver code ` `n ` `=` `15` `print` `(countPairs(n)) ` ` ` `# This code is contributed ` `# by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count pairs of numbers ` `// from 1 to N with Product divisible ` `// by their Sum ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count pairs ` `static` `int` `countPairs(` `int` `n) ` `{ ` ` ` `// variable to store count ` ` ` `int` `count = 0; ` ` ` ` ` `// Generate all possible pairs ` ` ` `// such that 1 <= x < y < n ` ` ` `for` `(` `int` `x = 1; x < n; x++) ` ` ` `{ ` ` ` `for` `(` `int` `y = x + 1; y <= n; y++) ` ` ` `{ ` ` ` `if` `((y * x) % (y + x) == 0) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 15; ` ` ` ` ` `Console.WriteLine(countPairs(n)); ` `} ` `} ` ` ` `// This code is contributed by anuj_67 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count pairs of ` `// numbers from 1 to N with Product ` `// divisible by their Sum ` ` ` `// Function to count pairs ` `function` `countPairs(` `$n` `) ` `{ ` ` ` `// variable to store count ` ` ` `$count` `= 0; ` ` ` ` ` `// Generate all possible pairs ` ` ` `// such that 1 <= x < y < n ` ` ` `for` `(` `$x` `= 1; ` `$x` `< ` `$n` `; ` `$x` `++) ` ` ` `{ ` ` ` `for` `(` `$y` `= ` `$x` `+ 1; ` `$y` `<= ` `$n` `; ` `$y` `++) ` ` ` `{ ` ` ` `if` `((` `$y` `* ` `$x` `) % (` `$y` `+ ` `$x` `) == 0) ` ` ` `$count` `++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$n` `= 15; ` `echo` `countPairs(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity :** O(N^{2})

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count sub-arrays whose product is divisible by k
- Print k numbers where all pairs are divisible by m
- Count pairs in array whose sum is divisible by K
- Check if product of first N natural numbers is divisible by their sum
- Sum and Product of all Composite numbers which are divisible by k in an array
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count number of pairs in array having sum divisible by K | SET 2
- Count ordered pairs with product less than N
- Count number of ordered pairs with Even and Odd Product
- Count all distinct pairs with product equal to K
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of pairs in an array whose product is a perfect square
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count of pairs in an array such that the highest power of 2 that divides their product is 1

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.