Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)

Given a number N, the task is to count all ‘a’ and ‘b’ that satisfy the condition a^2 + b^2 = N.

Note:- (a, b) and (b, a) are to be considered as two different pairs and (a, a) is also valid and to be considered only one time.

Examples:



Input: N = 10
Output:  2
1^2 + 3^2 = 9
3^2 + 1^2 = 9

Input: N = 8
Output: 1
2^2 + 2^2 = 8

Approach:

  1. Traverse numbers from 1 to square root of N.
    • Subtract square of the current number from N and check if their difference is a perfect square or not.
    • If it is perfect square then increment the count.
  2. Return count.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count pairs whose sum
// of squares is N
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the pairs satisfying
// a ^ 2 + b ^ 2 = N
int countPairs(int N)
{
    int count = 0;
  
    // Check for each number 1 to sqrt(N)
    for (int i = 1; i <= sqrt(N); i++) {
  
        // Store square of a number
        int sq = i * i;
  
        // Subtract the square from given N
        int diff = N - sq;
  
        // Check if the difference is also
        // a perfect square
        int sqrtDiff = sqrt(diff);
  
        // If yes, then increment count
        if (sqrtDiff * sqrtDiff == diff)
            count++;
    }
  
    return count;
}
  
// Driver code
int main()
{
    // Loop to Count no. of pairs satisfying
    // a ^ 2 + b ^ 2 = i for N = 1 to 10
    for (int i = 1; i <= 10; i++)
        cout << "For n = " << i << ", "
             << countPairs(i) << " pair exists\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count pairs whose sum 
// of squares is N 
  
import java.io.*;
  
class GFG {
  
  
  
// Function to count the pairs satisfying 
// a ^ 2 + b ^ 2 = N 
static int countPairs(int N) 
    int count = 0
  
    // Check for each number 1 to sqrt(N) 
    for (int i = 1; i <= (int)Math.sqrt(N); i++) 
    
  
        // Store square of a number 
        int sq = i * i; 
  
        // Subtract the square from given N 
        int diff = N - sq; 
  
        // Check if the difference is also 
        // a perfect square 
        int sqrtDiff = (int)Math.sqrt(diff); 
  
        // If yes, then increment count 
        if (sqrtDiff * sqrtDiff == diff) 
            count++; 
    
  
    return count; 
  
    // Driver code 
    public static void main (String[] args)
    {
    // Loop to Count no. of pairs satisfying 
    // a ^ 2 + b ^ 2 = i for N = 1 to 10 
    for (int i = 1; i <= 10; i++) 
        System.out.println( "For n = " + i + ", "
            + countPairs(i) + " pair exists\n"); 
    }
}
// This code is contributed by inder_verma.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count pairs whose sum 
# of squares is N 
  
# From math import everything
from math import *
  
# Function to count the pairs satisfying 
# a ^ 2 + b ^ 2 = N 
def countPairs(N) :
    count = 0
  
    # Check for each number 1 to sqrt(N) 
    for i in range(1, int(sqrt(N)) + 1) :
  
        # Store square of a number 
        sq = i * i
  
        # Subtract the square from given N
        diff = N - sq
  
        #  Check if the difference is also 
        # a perfect square 
        sqrtDiff = int(sqrt(diff))
  
        # If yes, then increment count
        if sqrtDiff * sqrtDiff == diff :
            count += 1
  
    return count
  
# Driver code     
if __name__ == "__main__" :
  
    # Loop to Count no. of pairs satisfying 
    # a ^ 2 + b ^ 2 = i for N = 1 to 10 
    for i in range(1,11) :
        print("For n =",i,", ",countPairs(i),"pair exists")
  
   
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count pairs whose sum 
// of squares is N 
   
  
using System; 
class GFG {
   
   
   
// Function to count the pairs satisfying 
// a ^ 2 + b ^ 2 = N 
static int countPairs(int N) 
    int count = 0; 
   
    // Check for each number 1 to Sqrt(N) 
    for (int i = 1; i <= (int)Math.Sqrt(N); i++) 
    
   
        // Store square of a number 
        int sq = i * i; 
   
        // Subtract the square from given N 
        int diff = N - sq; 
   
        // Check if the difference is also 
        // a perfect square 
        int sqrtDiff = (int)Math.Sqrt(diff); 
   
        // If yes, then increment count 
        if (sqrtDiff * sqrtDiff == diff) 
            count++; 
    
   
    return count; 
   
    // Driver code 
    public static void Main ()
    {
    // Loop to Count no. of pairs satisfying 
    // a ^ 2 + b ^ 2 = i for N = 1 to 10 
    for (int i = 1; i <= 10; i++) 
        Console.Write( "For n = " + i + ", "
            + countPairs(i) + " pair exists\n"); 
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count pairs 
// whose sum of squares is N 
  
// Function to count the pairs 
// satisfying a ^ 2 + b ^ 2 = N 
function countPairs($N
    $count = 0; 
    $i = 0;
      
    // Check for each number 1 to sqrt(N) 
    for ($i = 1; $i <= sqrt($N); $i++) 
    
  
        // Store square of a number 
        $sq = $i * $i
  
        // Subtract the square
        // from given N 
        $diff =$N - $sq
  
        // Check if the difference 
        // is also a perfect square 
        $sqrtDiff = sqrt($diff); 
  
        // If yes, then increment count 
        if ($sqrtDiff * $sqrtDiff == $diff
            $count++; 
    
  
    return $count
  
// Driver code 
  
// Loop to Count no. of pairs satisfying 
// a ^ 2 + b ^ 2 = i for N = 1 to 10 
for ($i = 1; $i <= 10; $i++) 
    echo "For n = " . $i . ", "
          countPairs($i) . " pair exists\n"
  
// This code is contributed by Raj
?>

chevron_right


Output:

For n = 1, 1 pair exists
For n = 2, 1 pair exists
For n = 3, 0 pair exists
For n = 4, 1 pair exists
For n = 5, 2 pair exists
For n = 6, 0 pair exists
For n = 7, 0 pair exists
For n = 8, 1 pair exists
For n = 9, 1 pair exists
For n = 10, 2 pair exists

Time Complexity : O(sqrt(N))



My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.