Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N - GeeksforGeeks

19 Dec, 2021
Given a positive integer N, the task is to find the number of ordered pairs (X, Y) where both X and Y are positive integers, such that they satisfy the equation 1/X + 1/Y = 1/N.
Examples:
Input: N = 5 Output: 3 Explanation: Only 3 pairs {(30,6), (10,10), (6,30)} satisfy the given equation. 
Input: N = 360 Output: 105  
Approach: Follow the steps to solve the problem:  
Solve for X using the given equation.  
1/X + 1/Y = 1/N => 1/X = 1/N – 1/Y => 1/X = (Y – N) / NY => X = NY / (Y – N) 
X = [ NY / (Y – N) ] * (1)
=> X = [ NY / (Y – N) ] * [1 – N/Y + N/Y]
=> X = [ NY / (Y – N) ] * [(Y- N)/Y + N/Y]
=> X = N + N2 / (Y – N)
Therefore, it can be observed that, to have a positive integer X, the remainder when N2 is divided by (Y – N) needs to be 0.
It can be observed that the minimum value of Y can be N + 1 (so that denominator Y – N > 0) and the maximum value of Y can be N2 + N so that N2/(Y – N) remains a positive integer ≥ 1.
Then iterate over the maximum and minimum possible values of Y, and for each value of Y for which N2 % (Y – N) == 0, increment count.
Finally, return count as the number of ordered pairs.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ Program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find number of ordered// positive integer pairs (x,y) such// that  they satisfy the equationvoid solve(int n){    // Initialize answer variable    int ans = 0; // Iterate over all possible values of y    for (int y = n + 1; y <= n * n + n; y++) {         // For valid x and y,        // (n*n)%(y - n) has to be 0        if ((n * n) % (y - n) == 0) {             // Increment count of ordered pairs            ans += 1;        }    }     // Print the answer    cout << ans;} // Driver Codeint main(){    int n = 5;    // Function call    solve(n);    return 0;}
// Java program for the above approachclass GFG{ // Function to find number of ordered// positive integer pairs (x,y) such// that they satisfy the equationstatic void solve(int n){         // Initialize answer variable    int ans = 0;     // Iterate over all possible values of y    for(int y = n + 1; y <= n * n + n; y++)    {                 // For valid x and y,        // (n*n)%(y - n) has to be 0        if ((n * n) % (y - n) == 0)        {                         // Increment count of ordered pairs            ans += 1;        }    }     // Print the answer    System.out.print(ans);} // Driver Codepublic static void main(String[] args){    int n = 5;         // Function call    solve(n);}} // This code is contributed by Amit Katiyar
# Python3 program for the above approach # Function to find number of ordered# positive integer pairs (x,y) such# that they satisfy the equationdef solve(n):     # Initialize answer variable    ans = 0     # Iterate over all possible values of y    y = n + 1    while(y <= n * n + n):         # For valid x and y,        # (n*n)%(y - n) has to be 0        if ((n * n) % (y - n) == 0):                         # Increment count of ordered pairs            ans += 1         y += 1     # Print the answer    print(ans) # Driver Coden = 5 # Function callsolve(n) # This code is contributed by Shivam Singh
// C# program for the above approachusing System; class GFG{ // Function to find number of ordered// positive integer pairs (x,y) such// that they satisfy the equationstatic void solve(int n){         // Initialize answer variable    int ans = 0;     // Iterate over all possible values of y    for(int y = n + 1; y <= n * n + n; y++)    {                 // For valid x and y,        // (n*n)%(y - n) has to be 0        if ((n * n) % (y - n) == 0)        {                         // Increment count of ordered pairs            ans += 1;        }    }     // Print the answer    Console.Write(ans);} // Driver Codepublic static void Main(String[] args){    int n = 5;         // Function call    solve(n);}} // This code is contributed by Amit Katiyar
<script>// javascript program for the above approach   // Function to find number of ordered    // positive integer pairs (x,y) such    // that they satisfy the equation    function solve(n) {         // Initialize answer variable        var ans = 0;         // Iterate over all possible values of y        for (y = n + 1; y <= n * n + n; y++) {             // For valid x and y,            // (n*n)%(y - n) has to be 0            if ((n * n) % (y - n) == 0) {                 // Increment count of ordered pairs                ans += 1;            }        }         // Print the answer        document.write(ans);    }     // Driver Code             var n = 5;         // Function call        solve(n); // This code contributed by umadevi9616</script>
Output:  
3
Time Complexity: O(logN)Auxiliary Space: O(1)
SHIVAMSINGH67
amit143katiyar
umadevi9616
yathaarthsuri
simranarora5sos
counter
Numbers
Mathematical
Puzzles
School Programming
Searching
Searching
Mathematical
Numbers
Puzzles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
SDE SHEET - A Complete Guide for SDE Preparation
Puzzle 1 | (How to Measure 45 minutes using two identical wires?)
Puzzle 2 | (Find ages of daughters)
Algorithm to solve Rubik's Cube
Puzzle 3 | (Calculate total distance travelled by bee)