Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to Find number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
#include <bits/stdc++.h>
using namespace std;
  
// function to ind number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
int Triplets(int n)
{
    // to store required answer
    int ans = 0;
  
    // run nested loops for first two numbers.
    for (int i = 1; i <= n; ++i) {
        for (int j = i; j <= n; ++j) {
            int x = i * i + j * j;
  
            // third number
            int y = sqrt(x);
  
            // check if third number is perfect
            // square and less than n
            if (y * y == x && y <= n)
                ++ans;
        }
    }
  
    return ans;
}
  
// Driver code
int main()
{
  
    int n = 10;
  
    // function call
    cout << Triplets(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to Find number of 
// Triplets 1 <= a <= b<= c <= n, 
// Such that a^2 + b^2 = c^2 
class Solution
{
// function to ind number of 
// Triplets 1 <= a <= b<= c <= n, 
// Such that a^2 + b^2 = c^2 
static int Triplets(int n) 
    // to store required answer 
    int ans = 0
  
    // run nested loops for first two numbers. 
    for (int i = 1; i <= n; ++i) { 
        for (int j = i; j <= n; ++j) { 
            int x = i * i + j * j; 
  
            // third number 
            int y =(int) Math.sqrt(x); 
  
            // check if third number is perfect 
            // square and less than n 
            if (y * y == x && y <= n) 
                ++ans; 
        
    
  
    return ans; 
  
// Driver code 
public static void main(String args[])
  
    int n = 10
  
    // function call 
    System.out.println(Triplets(n)); 
  
}
//contributed by Arnab Kundu

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to Find number of
# Triplets 1 <= a <= b<= c <= n,
# Such that a^2 + b^2 = c^2
import math
  
# function to ind number of
# Triplets 1 <= a <= b<= c <= n,
# Such that a^2 + b^2 = c^2
def Triplets(n):
  
    # to store required answer
    ans = 0
  
    # run nested loops for first two numbers.
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            x = i * i + j * j
  
            # third number
            y = int(math.sqrt(x))
  
            # check if third number is perfect
            # square and less than n
            if (y * y == x and y <= n):
                ans += 1
    return ans
  
# Driver code
if __name__ == "__main__":
    n = 10
  
    # function call
    print(Triplets(n))
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Find number of 
// Triplets 1 <= a <= b<= c <= n, 
// Such that a^2 + b^2 = c^2 
using System;
  
class GFG
{
// function to ind number of 
// Triplets 1 <= a <= b<= c <= n, 
// Such that a^2 + b^2 = c^2 
static int Triplets(int n) 
    // to store required answer 
    int ans = 0; 
  
    // run nested loops for first two numbers. 
    for (int i = 1; i <= n; ++i)
    
        for (int j = i; j <= n; ++j) 
        
            int x = i * i + j * j; 
  
            // third number 
            int y = (int)Math.Sqrt(x); 
  
            // check if third number is perfect 
            // square and less than n 
            if (y * y == x && y <= n) 
                ++ans; 
        
    
  
    return ans; 
  
// Driver code 
static void Main()
{
    int n = 10;
    Console.WriteLine(Triplets(n));
}
}
  
// This code is contributed by ANKITRAI1

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to Find number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
  
// Function to ind number of
// Triplets 1 <= a <= b<= c <= n,
// Such that a^2 + b^2 = c^2
function Triplets($n)
{
    // to store required answer
    $ans = 0;
  
    // run nested loops for first
    // two numbers.
    for ($i = 1; $i <= $n; ++$i)
    {
        for ($j =$i; $j <= $n; ++$j)
        {
            $x = $i * $i + $j * $j;
  
            // third number
            $y = (int)sqrt($x);
  
            // check if third number is perfect
            // square and less than n
            if ($y * $y == $x && $y <= $n)
                ++$ans;
        }
    }
  
    return $ans;
}
  
// Driver code
$n = 10;
  
// function call
echo Triplets($n);
  
// This code is contributed by mits
?>

chevron_right


Output:

2

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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
public class GFG {
  
    // Function to return an ArrayList containing
    // all the perfect squares upto n
    public static ArrayList<Integer> getPerfectSquares(int n)
    {
        ArrayList<Integer> perfectSquares = new ArrayList<>();
        int current = 1, i = 1;
  
        // while current perfect square is less than or equal to n
        while (current <= n) {
            perfectSquares.add(current);
            current = (int)Math.pow(++i, 2);
        }
        return perfectSquares;
    }
  
    // Function to return the count of triplet (a, b, c) pairs
    // such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
    public static int countTriplets(int n)
    {
        // List of perfect squares upto n^2
        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);
  
                // Since, a^2 + b^2 = c^2
                int bSquare = cSquare - aSquare;
                int b = (int)Math.sqrt(bSquare);
                int c = (int)Math.sqrt(cSquare);
  
                // If c < a or bSquare is not a perfect square
                if (c < a || !perfectSquares.contains(bSquare))
                    continue;
  
                // If triplet pair (a, b, c) satisfy the given condition
                if ((b >= a) && (b <= c) && (aSquare + bSquare == cSquare))
                    count++;
            }
        }
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 10;
  
        System.out.println(countTriplets(n));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System.Collections;
using System;
  
class GFG 
{
  
// Function to return an ArrayList containing
// all the perfect squares upto n
public static ArrayList getPerfectSquares(int n)
{
    ArrayList perfectSquares = new ArrayList();
    int current = 1, i = 1;
  
    // while current perfect square is less
    // than or equal to n
    while (current <= n) 
    {
        perfectSquares.Add(current);
        current = (int)Math.Pow(++i, 2);
    }
    return perfectSquares;
}
  
// Function to return the count of triplet 
// (a, b, c) pairs such that a^2 + b^2 = c^2 
// and 1 <= a <= b <= c <= n
public static int countTriplets(int n)
{
    // List of perfect squares upto n^2
    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];
  
            // Since, a^2 + b^2 = c^2
            int bSquare = cSquare - aSquare;
            int b = (int)Math.Sqrt(bSquare);
            int c = (int)Math.Sqrt(cSquare);
  
            // If c < a or bSquare is not a perfect square
            if (c < a || !perfectSquares.Contains(bSquare))
                continue;
  
            // If triplet pair (a, b, c) satisfy 
            // the given condition
            if ((b >= a) && (b <= c) && 
                (aSquare + bSquare == cSquare))
                count++;
        }
    }
    return count;
}
  
// Driver code
public static void Main()
{
    int n = 10;
  
    Console.WriteLine(countTriplets(n));
}
}
  
// This code is contributed by mits.

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.