Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
Last Updated :
31 Aug, 2022
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 = 10
3^2 + 1^2 = 10
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++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int N)
{
int count = 0;
for ( int i = 1; i <= sqrt (N); i++) {
int sq = i * i;
int diff = N - sq;
int sqrtDiff = sqrt (diff);
if (sqrtDiff * sqrtDiff == diff)
count++;
}
return count;
}
int main()
{
for ( int i = 1; i <= 10; i++)
cout << "For n = " << i << ", "
<< countPairs(i) << " pair exists\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPairs( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= ( int )Math.sqrt(N); i++)
{
int sq = i * i;
int diff = N - sq;
int sqrtDiff = ( int )Math.sqrt(diff);
if (sqrtDiff * sqrtDiff == diff)
count++;
}
return count;
}
public static void main (String[] args)
{
for ( int i = 1 ; i <= 10 ; i++)
System.out.println( "For n = " + i + ", "
+ countPairs(i) + " pair exists\n" );
}
}
|
Python 3
from math import *
def countPairs(N) :
count = 0
for i in range ( 1 , int (sqrt(N)) + 1 ) :
sq = i * i
diff = N - sq
sqrtDiff = int (sqrt(diff))
if sqrtDiff * sqrtDiff = = diff :
count + = 1
return count
if __name__ = = "__main__" :
for i in range ( 1 , 11 ) :
print ( "For n =" ,i, ", " ,countPairs(i), "pair exists" )
|
C#
using System;
class GFG {
static int countPairs( int N)
{
int count = 0;
for ( int i = 1; i <= ( int )Math.Sqrt(N); i++)
{
int sq = i * i;
int diff = N - sq;
int sqrtDiff = ( int )Math.Sqrt(diff);
if (sqrtDiff * sqrtDiff == diff)
count++;
}
return count;
}
public static void Main ()
{
for ( int i = 1; i <= 10; i++)
Console.Write( "For n = " + i + ", "
+ countPairs(i) + " pair exists\n" );
}
}
|
PHP
<?php
function countPairs( $N )
{
$count = 0;
$i = 0;
for ( $i = 1; $i <= sqrt( $N ); $i ++)
{
$sq = $i * $i ;
$diff = $N - $sq ;
$sqrtDiff = sqrt( $diff );
if ( $sqrtDiff * $sqrtDiff == $diff )
$count ++;
}
return $count ;
}
for ( $i = 1; $i <= 10; $i ++)
echo "For n = " . $i . ", " .
countPairs( $i ) . " pair exists\n" ;
?>
|
Javascript
<script>
function countPairs(N)
{
let count = 0;
for (let i = 1; i <= Math.sqrt(N); i++) {
let sq = i * i;
let diff = N - sq;
let sqrtDiff = Math.sqrt(diff);
if (sqrtDiff * sqrtDiff == diff)
count++;
}
return count;
}
for (let i = 1; i <= 10; i++)
document.write( "For n = " + i + ", "
+ countPairs(i) + " pair exists<br>" );
</script>
|
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))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...