Count number of triangles possible with length of sides not exceeding N
Last Updated :
23 Feb, 2023
Given an integer N, the task is to find the total number of right angled triangles that can be formed such that the length of any side of the triangle is at most N.
A right-angled triangle satisfies the following condition: X2 + Y2 = Z2 where Z represents the length of the hypotenuse, and X and Y represent the lengths of the remaining two sides.
Examples:
Input: N = 5
Output: 1
Explanation:
The only possible combination of sides which form a right-angled triangle is {3, 4, 5}.
Input: N = 10
Output: 2
Explanation:
Possible combinations of sides which form a right-angled triangle are {3, 4, 5} and {6, 8, 10}.
Naive Approach: The idea is to generate every possible combination of triplets with integers from the range [1, N] and for each such combination, check whether it is a right-angled triangle or not.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int right_angled( int n)
{
int count = 0;
for ( int z = 1; z <= n; z++) {
for ( int y = 1; y <= z; y++) {
for ( int x = 1; x <= y; x++) {
if ((x * x) + (y * y) == (z * z)) {
count++;
}
}
}
}
return count;
}
int main()
{
int n = 5;
cout << right_angled(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int right_angled( int n)
{
int count = 0 ;
for ( int z = 1 ; z <= n; z++)
{
for ( int y = 1 ; y <= z; y++)
{
for ( int x = 1 ; x <= y; x++)
{
if ((x * x) + (y * y) == (z * z))
{
count++;
}
}
}
}
return count;
}
public static void main (String[] args)
{
int n = 5 ;
System.out.println(right_angled(n));
}
}
|
Python3
def right_angled(n):
count = 0
for z in range ( 1 , n + 1 ):
for y in range ( 1 , z + 1 ):
for x in range ( 1 , y + 1 ):
if ((x * x) + (y * y) = = (z * z)):
count + = 1
return count
n = 5
print (right_angled(n))
|
C#
using System;
class GFG{
static int right_angled( int n)
{
int count = 0;
for ( int z = 1; z <= n; z++)
{
for ( int y = 1; y <= z; y++)
{
for ( int x = 1; x <= y; x++)
{
if ((x * x) + (y * y) == (z * z))
{
count++;
}
}
}
}
return count;
}
public static void Main( string [] args)
{
int n = 5;
Console.Write(right_angled(n));
}
}
|
Javascript
<script>
function right_angled(n)
{
var count = 0;
for (z = 1; z <= n; z++)
{
for (y = 1; y <= z; y++)
{
for (x = 1; x <= y; x++)
{
if ((x * x) + (y * y) == (z * z))
{
count++;
}
}
}
}
return count;
}
var n = 5;
document.write(right_angled(n));
</script>
|
Time complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the idea that the third side of the triangle can be found out, if the two sides of the triangles are known. Follow the steps below to solve the problem:
- Iterate up to N and generate pairs of possible length of two sides and find the third side using the relation x2 + y2 = z2
- If sqrt(x2+y2) is found to be an integer, store the three concerned integers in a Set in sorted order, as they can form a right angled triangle.
- Print the final size of the set as the required count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int right_angled( int n)
{
set<pair< int , pair< int , int > > > s;
for ( int x = 1; x <= n; x++) {
for ( int y = 1; y <= n; y++) {
if (x * x + y * y <= n * n) {
int z = sqrt (x * x + y * y);
if (z * z != (x * x + y * y))
continue ;
vector< int > v;
v.push_back(x);
v.push_back(y);
v.push_back( sqrt (x * x + y * y));
sort(v.begin(), v.end());
s.insert({ v[0], { v[1], v[2] } });
}
else
break ;
}
}
return s.size();
}
int main()
{
int n = 5;
cout << right_angled(n);
return 0;
}
|
Java
import java.util.*;
class Pair<F, S>
{
private F first;
private S second;
public Pair(F first, S second)
{
this .first = first;
this .second = second;
}
}
class GFG{
public static int right_angled( int n)
{
Set<Pair<Integer,
Pair<Integer,
Integer>>> s = new HashSet<Pair<Integer,
Pair<Integer,
Integer>>>();
for ( int x = 1 ; x <= n; x++)
{
for ( int y = 1 ; y <= n; y++)
{
if (x * x + y * y <= n * n)
{
int z = ( int )Math.sqrt(x * x + y * y);
if (z * z != (x * x + y * y))
continue ;
Vector<Integer> v = new Vector<Integer>();
v.add(x);
v.add(y);
v.add(( int )Math.sqrt(x * x + y * y));
Collections.sort(v);
s.add( new Pair<Integer,
Pair<Integer,
Integer>>(v.get( 0 ),
new Pair<Integer,
Integer>(v.get( 1 ),
v.get( 2 ))));
}
else
break ;
}
}
return s.size() - 1 ;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(right_angled(n));
}
}
|
Python3
import math
def right_angled(n):
s = {}
for x in range ( 1 ,n + 1 ):
for y in range ( 1 ,n + 1 ):
if (x * x + y * y< = n * n):
z = int (math.sqrt(x * x + y * y))
if (z * z! = (x * x + y * y)):
continue
v = []
v.append(x)
v.append(y)
v.append( int (math.sqrt(x * x + y * y)))
v.sort()
s[v[ 0 ]] = [v[ 1 ],v[ 2 ]]
else :
break
return len (s)
n = 5
print (right_angled(n))
|
Javascript
function right_angled(n)
{
let s = new Set();
for (let x = 1; x <= n; x++) {
for (let y = 1; y <= n; y++) {
if (x * x + y * y <= n * n) {
let z = Math.floor(Math.sqrt(x * x + y * y));
if (z * z != (x * x + y * y))
continue ;
let v = new Array();
v.push(x);
v.push(y);
v.push(Math.floor(Math.sqrt(x * x + y * y)));
v.sort();
s.add([v[0],[v[1], v[2]]].join());
}
else
break ;
}
}
return s.size;
}
let n = 5;
console.log(right_angled(n));
|
C#
using System;
using System.Collections.Generic;
class Pair<F, S> {
private F first;
private S second;
public Pair(F first, S second)
{
this .first = first;
this .second = second;
}
}
class GFG {
public static int right_angled( int n)
{
HashSet<Pair< int , Pair< int , int > > > s
= new HashSet<Pair< int , Pair< int , int > > >();
for ( int x = 1; x <= n; x++) {
for ( int y = 1; y <= n; y++) {
if (x * x + y * y <= n * n) {
int z = ( int )Math.Sqrt(x * x + y * y);
if (z * z != (x * x + y * y))
continue ;
List< int > v = new List< int >();
v.Add(x);
v.Add(y);
v.Add(( int )Math.Sqrt(x * x + y * y));
v.Sort();
s.Add( new Pair< int , Pair< int , int > >(
v[0],
new Pair< int , int >(v[1], v[2])));
}
else
break ;
}
}
return s.Count - 1;
}
public static void Main( string [] args)
{
int n = 5;
Console.WriteLine(right_angled(n));
}
}
|
Time complexity: O(N2*log(N)) as using sqrt inside inner for loop
Auxiliary Space: O(N) since using auxiliary space for set
Share your thoughts in the comments
Please Login to comment...