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:
- 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.
- Return count.
Below is the implementation of above approach:
C++
// 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
filter_none
Java
// 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
filter_none
Python 3
# 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
filter_none
C#
// 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
filter_none
PHP
<?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
filter_none
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))
Recommended Posts:
- Count Magic squares in a grid
- Count number of squares in a rectangle
- Count number less than N which are product of perfect squares
- Count squares with odd side length in Chessboard
- Count the total number of squares that can be visited by Bishop in one move
- Program to count number of distinct Squares and Cubes upto N
- Count pairs with given sum | Set 2
- Count pairs with Odd XOR
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count Pairs from two arrays with even sum
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count of pairs (x, y) in an array such that x < y
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
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.



