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 = 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++
// 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
// 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
# 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#
// 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 |
Output:
3
Time Complexity: O(logN)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Smallest positive integer X satisfying the given equation
- Sum of minimum value of x and y satisfying the equation ax + by = c
- Count valid pairs in the array satisfying given conditions
- Count of pairs satisfying the given condition
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Count ordered pairs with product less than N
- Count ordered pairs of numbers with a given LCM
- Find the number of ordered pairs such that a * p + b * q = N, where p and q are primes
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Count of Fibonacci pairs which satisfy the given equation
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
- Count possible permutations of given array satisfying the given conditions
- Count of ordered triplets (R, G, B) in a given original string
- Find x and y satisfying ax + by = n
- Check if elements of an array can be arranged satisfying the given condition
- Generate N integers satisfying the given conditions
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.

