Pair of integers (a, b) which satisfy the given equations

30 Jan, 2022
Given the system of equations a2 + b = n and a + b2 = m. The task is to find the number of pair of positive integers (a, b) which satisfy the equation for given n and m.Examples:  
Input: n = 9, m = 3 Output: 1 Explanation: Only one pair (3, 0) exists for both equations satisfying the conditions.Input: n = 4, m = 20 Output: 0 Explanation: There are no such pair exists.  
 
Approach: The approach is to check for all possible pairs of numbers and check if that pair satisfy both the equations or not. For this we have,  
   a2 + b = n ... (1)
   a + b2 = m ... (2)
For equation (2), 
=> a = m - b2 ... (3)
 
Now for the positive value of a, every value of b must be from 0 to sqrt(m).
Obtain the value of a from equations (3).
If the pair (a, b) satisfy equation (1), then pair (a, b) is the solution of system of equations.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to count the pair of integers(a, b)// which satisfy the equation// a^2 + b = n and a + b^2 = m#include <bits/stdc++.h>using namespace std; // Function to count valid pairsint pairCount(int n, int m){    int cnt = 0, b, a;    for (b = 0; b <= sqrt(m); b++) {        a = m - b * b;        if (a * a + b == n) {            cnt++;        }    }    return cnt;} // Driver codeint main(){    int n = 9, m = 3;     cout << pairCount(n, m) << endl;     return 0;}
// Java program to count the pair of integers(a, b)// which satisfy the equation// a^2 + b = n and a + b^2 = mclass GFG{ // Function to count valid pairsstatic int pairCount(int n, int m){    int cnt = 0, b, a;    for (b = 0; b <= Math.sqrt(m); b++)    {        a = m - b * b;        if (a * a + b == n)        {            cnt++;        }    }    return cnt;} // Driver codepublic static void main(String[] args){    int n = 9, m = 3;    System.out.print(pairCount(n, m) +"\n");}} // This code is contributed by 29AjayKumar
# Python3 program to count the pair of integers(a, b)# which satisfy the equation# a^2 + b = n and a + b^2 = m # Function to count valid pairsdef pairCount(n, m):    cnt = 0;    for b in range(int(pow(m, 1/2))):        a = m - b * b;        if (a * a + b == n):            cnt += 1;             return cnt; # Driver codeif __name__ == '__main__':    n = 9;    m = 3;    print(pairCount(n, m));     # This code is contributed by 29AjayKumar
// C# program to count the pair of integers(a, b)// which satisfy the equation// a^2 + b = n and a + b^2 = musing System; class GFG{ // Function to count valid pairsstatic int pairCount(int n, int m){    int cnt = 0, b, a;    for (b = 0; b <= Math.Sqrt(m); b++)    {        a = m - b * b;        if (a * a + b == n)        {            cnt++;        }    }    return cnt;} // Driver codepublic static void Main(String[] args){    int n = 9, m = 3;    Console.Write(pairCount(n, m) +"\n");}} // This code is contributed by 29AjayKumar
<script>// javascript program to count the pair of integers(a, b)// which satisfy the equation// a^2 + b = n and a + b^2 = m // Function to count valid pairsfunction pairCount(n , m){    var cnt = 0, b, a;    for (b = 0; b <= Math.sqrt(m); b++)    {        a = m - b * b;        if (a * a + b == n)        {            cnt++;        }    }    return cnt;} // Driver codevar n = 9, m = 3;document.write(pairCount(n, m)); // This code is contributed by Amit Katiyar</script>
1
 
Time Complexity: O(sqrt(min(n,m))
Auxiliary Space: O(1) 
29AjayKumar
amit143katiyar
subham348
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Modular multiplicative inverse
Find first and last digits of a number
Find cubic root of a number
Program to add two binary strings
Count all possible paths from top left to bottom right of a mXn matrix
Algorithm to solve Rubik's Cube
Program for distance between two points on earth
Pascal's Triangle
Segment Tree | Set 1 (Sum of given range)
Postfix to Infix