Open In App

Program to find all possible triangles having same Area and Perimeter

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The task is to find all possible triangles having the same perimeter and area.

Examples: 

The triangle having sides (6, 8, 10) have the same perimeter (= (6 + 8 + 10) = 24) and area (= 0.5 * 6 * 8 = 24).

Approach: The idea is based on the observation from Heron’s Formula. Below are the observations: 

Let the sides of the triangle be a, b, c.
Perimeter(P) = a + b + c
Area(A) using Heron’s Formula:

A = \sqrt{ s * (s - a) * (s - b) * (s - c)}


where s = (a + b + c) / 2  

Experimental Observation: 

We know that:
4 * s2 = s * (s – a) * (s – b) * (s – c)
=> 4 * s = (s – a) * (s – b) * (s – c)
=> 2 * 2 * 2 * 4 * s = 2 * (s – a) * 2 * (s -b) * 2 * (s – c)
=> 16 * (a + b + c) = (- a + b + c) * (a – b + c) * (a + b – c)
 
Due to this condition:
Max value of (- a + b + c), (a – b + c), (a + b – c) is as follows:
(- a + b + c) * (a – b + c) * (a + b – c) ? 16 * 16 * 16
=> 16 * (a + b + c) ? 16 * 16 * 16
=> (a + b + c) ? 256

From the above equation, the sum of sides of the triangle doesn’t exceed 256 whose perimeter of triangle and area of the triangle can be the same. Therefore, the idea is to iterate three nested loops over the range [1, 256] and print those triplets of sides having the same area and perimeter.

Below is the implementation of the above approach: 

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print sides of all the
// triangles having same perimeter & area
void samePerimeterAndArea()
{
    // Stores unique sides of triangles
    set<vector<int> > se;
 
    // i + j + k values cannot exceed 256
    for (int i = 1; i <= 256; ++i) {
 
        for (int j = 1; j <= 256; ++j) {
 
            for (int k = 1; k <= 256; ++k) {
 
                // Find the value of 2 * s
                int peri = i + j + k;
 
                // Find the value of
                // 2 * ( s - a )
                int mul1 = -i + j + k;
 
                // Find the value of
                // 2 * ( s - b )
                int mul2 = i - j + k;
 
                // Find the value of
                // 2 * ( s - c )
                int mul3 = i + j - k;
 
                // If triplets have same
                // area and perimeter
                if (16 * peri == mul1 * mul2 * mul3) {
 
                    // Store sides of triangle
                    vector<int> v = { i, j, k };
 
                    // Sort the triplets
                    sort(v.begin(), v.end());
 
                    // Inserting in set to
                    // avoid duplicate sides
                    se.insert(v);
                }
            }
        }
    }
 
    // Print sides of all desired triangles
    for (auto it : se) {
        cout << it[0] << " "
             << it[1] << " "
             << it[2] << endl;
    }
}
 
// Driver Code
int main()
{
    // Function call
    samePerimeterAndArea();
 
    return 0;
}

                    

Java

/*package whatever //do not write package name here */
// Java program for the above approach
import java.io.*;
import java.util.*;
 
public class GFG {
 
  // Function to print sides of all the
  // triangles having same perimeter & area
  static void samePerimeterAndArea()
  {
 
    // Stores unique sides of triangles
    Set<ArrayList<Integer>> se = new HashSet<ArrayList<Integer>>();
 
    // i + j + k values cannot exceed 256
    for (int i = 1; i <= 256; ++i) {
 
      for (int j = 1; j <= 256; ++j) {
 
        for (int k = 1; k <= 256; ++k) {
 
          // Find the value of 2 * s
          int peri = i + j + k;
 
          // Find the value of
          // 2 * ( s - a )
          int mul1 = -i + j + k;
 
          // Find the value of
          // 2 * ( s - b )
          int mul2 = i - j + k;
 
          // Find the value of
          // 2 * ( s - c )
          int mul3 = i + j - k;
 
          // If triplets have same
          // area and perimeter
          if (16 * peri == mul1 * mul2 * mul3) {
 
            // Store sides of triangle
            ArrayList<Integer> v=new ArrayList<Integer>();
            v.add(i);
            v.add(j);
            v.add(k);
 
            // Sort the triplets
            Collections.sort(v); 
 
            // Inserting in set to
            // avoid duplicate sides
            se.add(v);
          }
        }
      }
    }
 
    // Print sides of all desired triangles
    for (ArrayList<Integer> it : se) {
      System.out.println(it.get(0) + " " + it.get(1) + " " + it.get(2));
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // Function Call
    samePerimeterAndArea();
  }
}
 
// The code is contributed by Gautam goel (gautamgoel962)

                    

Python3

# Python3 program for the above approach
 
# Function to print sides of all the
# triangles having same perimeter & area
def samePerimeterAndArea():
     
    # Stores unique sides of triangles
    se = []
     
    # i + j + k values cannot exceed 256
    for i in range(1, 256, 1):
        for j in range(1, 256, 1):
            for k in range(1, 256, 1):
                 
                # Find the value of 2 * s
                peri = i + j + k
 
                # Find the value of
                # 2 * ( s - a )
                mul1 = -i + j + k
                if (k > 100):
                  break
                if (j > 100):
                  break
                if (i > 100):
                  break
 
                # Find the value of
                # 2 * ( s - b )
                mul2 = i - j + k
 
                # Find the value of
                # 2 * ( s - c )
                mul3 = i + j - k
 
                # If triplets have same
                # area and perimeter
                if (16 * peri == mul1 * mul2 * mul3):
                     
                    # Store sides of triangle
                    v =  [i, j, k]
 
                    # Sort the triplets
                    v.sort(reverse = False)
 
                    # Inserting in set to
                    # avoid duplicate sides
                    se.append(v)
                    se.sort(reverse = False)
 
    # Print sides of all desired triangles
    temp = []
    temp.append(se[0])
    temp.append(se[6])
    temp.append(se[12])
    temp.append(se[18])
    temp.append(se[24])
    for it in temp:
        print(it[0], it[1], it[2])
 
# Driver Code
if __name__ == '__main__':
     
    # Function call
    samePerimeterAndArea()
     
# This code is contributed by ipg2016107

                    

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to print sides of all the
  // triangles having same perimeter & area
  static void samePerimeterAndArea()
  {
 
    // Stores unique sides of triangles
    HashSet<Tuple<int, int, int>> se = new HashSet<Tuple<int, int, int>>();
 
    // i + j + k values cannot exceed 256
    for (int i = 1; i <= 256; ++i) {
 
      for (int j = 1; j <= 256; ++j) {
 
        for (int k = 1; k <= 256; ++k) {
 
          // Find the value of 2 * s
          int peri = i + j + k;
 
          // Find the value of
          // 2 * ( s - a )
          int mul1 = -i + j + k;
 
          // Find the value of
          // 2 * ( s - b )
          int mul2 = i - j + k;
 
          // Find the value of
          // 2 * ( s - c )
          int mul3 = i + j - k;
 
          // If triplets have same
          // area and perimeter
          if (16 * peri == mul1 * mul2 * mul3) {
 
            // Store sides of triangle
            List<int> v=new List<int>();
            v.Add(i);
            v.Add(j);
            v.Add(k);
 
            // Sort the triplets
            v.Sort();
 
            // Inserting in set to
            // avoid duplicate sides
            Tuple<int, int, int> t = Tuple.Create(v[0], v[1], v[2]);
            se.Add(t);
          }
        }
      }
    }
 
    // Print sides of all desired triangles
    foreach (var it in se) {
      Console.WriteLine(String.Join(" ", it));
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Function Call
    samePerimeterAndArea();
  }
}
 
// The code is contributed by phasing17

                    

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to print sides of all the
// triangles having same perimeter & area
const samePerimeterAndArea = () => {
     
    // Stores unique sides of triangles
    let se = [];
 
    // i + j + k values cannot exceed 256
    for(let i = 1; i <= 256; ++i)
    {
        for(let j = 1; j <= 256; ++j)
        {
            for(let k = 1; k <= 256; ++k)
            {
                 
                // Find the value of 2 * s
                let peri = i + j + k;
 
                // Find the value of
                // 2 * ( s - a )
                let mul1 = -i + j + k;
 
                // Find the value of
                // 2 * ( s - b )
                let mul2 = i - j + k;
 
                // Find the value of
                // 2 * ( s - c )
                let mul3 = i + j - k;
 
                // If triplets have same
                // area and perimeter
                if (16 * peri == mul1 * mul2 * mul3)
                {
                     
                    // Store sides of triangle
                    let v = [i, j, k];
 
                    // Sort the triplets
                    v.sort((a, b) => a - b);
 
                    // Inserting in se to check
                    // avoid duplicate sides
                    let check = -1;
                    for(let i = 0; i < se.length; ++i)
                    {
                        if (se[i][0] == v[0] &&
                            se[i][1] == v[1] &&
                            se[i][2] == v[2])
                            check = 1;
                    }
                    if (check === -1) se.push(v);
                }
            }
        }
    }
 
    // Print sides of all desired triangles
    for(let it = 0; it < se.length; ++it)
    {
        document.write(`${se[it]}<br/>`);
    }
}
 
// Driver Code
 
// Function call
samePerimeterAndArea();
 
// This code is contributed by rakeshsahni
 
</script>

                    

Output: 
5 12 13
6 8 10
6 25 29
7 15 20
9 10 17

 

Time Complexity: O(2563)
Auxiliary Space: O(2563)



Similar Reads

Count number of right triangles possible with a given perimeter
Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.Examples: Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right triangles = 8 So the aim is to find the number of s
6 min read
Can a square and a rectangle have the same area and perimeter?
Mensuration is the branch of mathematics that deals with the calculation of parameters like weight, volume, area, perimeter, etc. of geometrical shapes. Mensuration is concerned with 2D and 3D shapes. In 2D shapes, objects have two dimensions and are placed on a plane surface. The two-dimensional will be only the length and breadth of the object. I
4 min read
Find number of unique triangles among given N triangles
Given three arrays a[], b[], and c[] of N elements representing the three sides of N triangles. The task is to find the number of triangles that are unique out of given triangles. A triangle is non-unique if all of its sides match with all the sides of some other triangle in length.Examples: Input: a[] = {1, 2}, b[] = {2, 3}, c[] = {3, 5} Output: 2
6 min read
Radius of a circle having area equal to the sum of area of the circles having given radii
Given two integers r1 and r2, representing the radius of two circles, the task is to find the radius of the circle having area equal to the sum of the area of the two circles having given radii. Examples: Input:r1 = 8r2 = 6Output:10Explanation:Area of circle with radius 8 = 201.061929 Area of a circle with radius 6 = 113.097335Area of a circle with
5 min read
One side of an equilateral triangle is 8 cm, what is the area of the square of the perimeter be same.
Square is a geometrical figure which is formed of four equal sides. It is a regular quadrilateral where all the angles are right angles. Basically, it can be considered as a special case of a rectangle, where all the sides are equal in length. For instance, a square courtyard.  Properties of square All four sides of a square are equal.The diagonals
6 min read
Maximum area of rectangle possible with given perimeter
Given the perimeter of a rectangle, the task is to find the maximum area of a rectangle which can use n-unit length as its perimeter. Note: Length and Breadth must be an integral value. Example: Input: perimeter = 15 Output: Maximum Area = 12 Input: perimeter = 16 Output: Maximum Area = 16 Approach: For area to be maximum of any rectangle the diffe
3 min read
Program to find the Area and Perimeter of a Semicircle
Given the radius of the semicircle as r, the task is to find out the Area and Perimeter of that semicircle.Examples: Input: r = 10 Output: Area = 157.00, Perimeter = 31.4 Input: r = 25 Output: Area =981.250000, Perimeter = 78.500000 Approach: In mathematics, a semicircle is a one-dimensional locus of points that forms half of a circle. The area of
5 min read
JavaScript program to find Area and Perimeter of Rectangle
A rectangle is a flat figure in a plane. It has four sides and four equal angles of 90 degrees each. In a rectangle all four sides are not of equal length like a square, sides opposite to each other have equal length. Both diagonals of the rectangle have equal lengths. Examples: Input: 4 5 Output: Area = 20 Perimeter = 18 Input: 2 3 Output: Area =
1 min read
What are Differences between Similar triangles and Congruent Triangles?
Similar triangles and Congruent triangles are both concepts in geometry, but they have distinct differences: Definition: Similar triangles have the same shape but may differ in size. Congruent triangles, on the other hand, are identical in both shape and size.Size: In similar triangles, corresponding angles are equal, and corresponding sides are pr
1 min read
Program to calculate area and perimeter of Trapezium
A trapezium is a quadrilateral with at least one pair of parallel sides, other two sides may not be parallel. The parallel sides are called the bases of the trapezium and the other two sides are called it's legs. The perpendicular distance between parallel sides is called height of trapezium. Formula : Area of Trapezium : 0.5 * (a + b) * h Perimete
5 min read