Open In App

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

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:
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: 
 

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)

Below is the implementation of the above approach:




// 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 equation
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
    cout << ans;
}
 
// Driver Code
int main()
{
    int n = 5;
    // Function call
    solve(n);
    return 0;
}




// Java program for the above approach
class GFG{
 
// Function to find number of ordered
// positive integer pairs (x,y) such
// that they satisfy the equation
static 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 Code
public 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 equation
def 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 Code
n = 5
 
# Function call
solve(n)
 
# This code is contributed by Shivam Singh




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find number of ordered
// positive integer pairs (x,y) such
// that they satisfy the equation
static 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 Code
public 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(N2)
Auxiliary Space: O(1)


Article Tags :