Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
Last Updated :
07 Jan, 2024
Given an integer N, the task is to count the number of triplets (a, b, c) such that a2 + b2 = c2 and 1 <= a <= b <= c <= N.
Examples:
Input: N = 5
Output: 1
The only possible triplet pair is (3, 4, 5)
3^2 + 4^2 = 5^2 i.e. 9 + 16 = 25
Input: N = 10
Output: 2
(3, 4, 5) and (6, 8, 10) are the required triplet pairs.
Method-1:
Run two loop one from i = 1 to N and second from j = i+1 to N. Consider each and every pair and find i*i + j*j and check if this is a perfect square and its square root is less than N. If yes then increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Triplets( int n)
{
int ans = 0;
for ( int i = 1; i <= n; ++i) {
for ( int j = i; j <= n; ++j) {
int x = i * i + j * j;
int y = sqrt (x);
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
int main()
{
int n = 10;
cout << Triplets(n);
return 0;
}
|
Java
class Solution
{
static int Triplets( int n)
{
int ans = 0 ;
for ( int i = 1 ; i <= n; ++i) {
for ( int j = i; j <= n; ++j) {
int x = i * i + j * j;
int y =( int ) Math.sqrt(x);
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(Triplets(n));
}
}
|
Python3
import math
def Triplets(n):
ans = 0
for i in range ( 1 , n + 1 ):
for j in range (i, n + 1 ):
x = i * i + j * j
y = int (math.sqrt(x))
if (y * y = = x and y < = n):
ans + = 1
return ans
if __name__ = = "__main__" :
n = 10
print (Triplets(n))
|
C#
using System;
class GFG
{
static int Triplets( int n)
{
int ans = 0;
for ( int i = 1; i <= n; ++i)
{
for ( int j = i; j <= n; ++j)
{
int x = i * i + j * j;
int y = ( int )Math.Sqrt(x);
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
static void Main()
{
int n = 10;
Console.WriteLine(Triplets(n));
}
}
|
Javascript
<script>
function Triplets(n)
{
var ans = 0;
for (let i = 1; i <= n; ++i) {
for (let j = i; j <= n; ++j) {
var x = i * i + j * j;
var y = parseInt( Math.sqrt(x));
if (y * y == x && y <= n)
++ans;
}
}
return ans;
}
var n = 10;
document.write(Triplets(n));
</script>
|
PHP
<?php
function Triplets( $n )
{
$ans = 0;
for ( $i = 1; $i <= $n ; ++ $i )
{
for ( $j = $i ; $j <= $n ; ++ $j )
{
$x = $i * $i + $j * $j ;
$y = (int)sqrt( $x );
if ( $y * $y == $x && $y <= $n )
++ $ans ;
}
}
return $ans ;
}
$n = 10;
echo Triplets( $n );
?>
|
Time Complexity: O(n2sqrtx) where x is the max value of the third number.
Auxiliary Space: O(1)
Method-2:
- Find all the perfect squares upto n2 and save it to an ArrayList.
- Now for every a from 1 to n, do the following:
- Choose c2 from the list of perfect squares calculated earlier.
- Then b2 can be calculated as b2 = c2 – a2.
- Now check if a <= b <= c and b2 calculated in the previous step must be a perfect square.
- If the above conditions are satisfied then increment the count.
- Print the count in the end.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
vector< int > getPerfectSquares( int n)
{
vector< int > perfectSquares;
int current = 1, i = 1;
while (current <= n)
{
perfectSquares.push_back(current);
current = pow (++i, 2);
}
return perfectSquares;
}
int countTriplets( int n)
{
vector< int > perfectSquares = getPerfectSquares(
pow (n, 2));
int count = 0;
for ( int a = 1; a <= n; a++)
{
int aSquare = pow (a, 2);
for ( int i = 0; i < perfectSquares.size(); i++)
{
int cSquare = perfectSquares[i];
int bSquare = abs (cSquare - aSquare);
int b = sqrt (bSquare);
int c = sqrt (cSquare);
if (c < a || (find(perfectSquares.begin(),
perfectSquares.end(),
bSquare) ==
perfectSquares.end()))
continue ;
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
int main()
{
int n = 10;
cout << countTriplets(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static ArrayList<Integer> getPerfectSquares( int n)
{
ArrayList<Integer> perfectSquares = new ArrayList<>();
int current = 1 , i = 1 ;
while (current <= n) {
perfectSquares.add(current);
current = ( int )Math.pow(++i, 2 );
}
return perfectSquares;
}
public static int countTriplets( int n)
{
ArrayList<Integer> perfectSquares
= getPerfectSquares(( int )Math.pow(n, 2 ));
int count = 0 ;
for ( int a = 1 ; a <= n; a++) {
int aSquare = ( int )Math.pow(a, 2 );
for ( int i = 0 ; i < perfectSquares.size(); i++) {
int cSquare = perfectSquares.get(i);
int bSquare = cSquare - aSquare;
int b = ( int )Math.sqrt(bSquare);
int c = ( int )Math.sqrt(cSquare);
if (c < a || !perfectSquares.contains(bSquare))
continue ;
if ((b >= a) && (b <= c) && (aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println(countTriplets(n));
}
}
|
Python3
import math
def getPerfectSquares(n):
perfectSquares = []
current = 1
i = 1
while (current < = n) :
perfectSquares.append(current)
i + = 1
current = i * * 2
return perfectSquares
def countTriplets(n):
perfectSquares = getPerfectSquares(n * * 2 )
count = 0
for a in range ( 1 , n + 1 ):
aSquare = a * * 2
for i in range ( len (perfectSquares)):
cSquare = perfectSquares[i]
bSquare = abs (cSquare - aSquare)
b = math.sqrt(bSquare)
b = int (b)
c = math.sqrt(cSquare)
c = int (c)
if (c < a or (bSquare not in perfectSquares)):
continue
if ((b > = a) and (b < = c) and (aSquare + bSquare = = cSquare)):
count + = 1
return count
if __name__ = = "__main__" :
n = 10
print (countTriplets(n))
|
C#
using System.Collections;
using System;
class GFG
{
public static ArrayList getPerfectSquares( int n)
{
ArrayList perfectSquares = new ArrayList();
int current = 1, i = 1;
while (current <= n)
{
perfectSquares.Add(current);
current = ( int )Math.Pow(++i, 2);
}
return perfectSquares;
}
public static int countTriplets( int n)
{
ArrayList perfectSquares = getPerfectSquares(( int )Math.Pow(n, 2));
int count = 0;
for ( int a = 1; a <= n; a++)
{
int aSquare = ( int )Math.Pow(a, 2);
for ( int i = 0; i < perfectSquares.Count; i++)
{
int cSquare = ( int )perfectSquares[i];
int bSquare = cSquare - aSquare;
int b = ( int )Math.Sqrt(bSquare);
int c = ( int )Math.Sqrt(cSquare);
if (c < a || !perfectSquares.Contains(bSquare))
continue ;
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
public static void Main()
{
int n = 10;
Console.WriteLine(countTriplets(n));
}
}
|
Javascript
<script>
function getPerfectSquares(n)
{
var perfectSquares = [];
var current = 1, i = 1;
while (current <= n)
{
perfectSquares.push(current);
current = Math.pow(++i, 2);
}
return perfectSquares;
}
function countTriplets(n)
{
var perfectSquares = getPerfectSquares(
Math.pow(n, 2));
var count = 0;
for ( var a = 1; a <= n; a++)
{
var aSquare = Math.pow(a, 2);
for ( var i = 0;
i < perfectSquares.length;
i++)
{
var cSquare = perfectSquares[i];
var bSquare = Math.abs(cSquare -
aSquare);
var b = Math.sqrt(bSquare);
var c = Math.sqrt(cSquare);
if (c < a ||
!perfectSquares.includes(bSquare))
continue ;
if ((b >= a) && (b <= c) &&
(aSquare + bSquare == cSquare))
count++;
}
}
return count;
}
var n = 10;
document.write(countTriplets(n));
</script>
|
Time Complexity: O(n*nlogn)
Auxiliary Space: O(logn)
Share your thoughts in the comments
Please Login to comment...