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 + cArea(A)using Heron’s Formula:

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

__Experimental Observation__:

We know that:

4 * s^{2}= 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

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

**Time Complexity:** O(256^{3})**Auxiliary Space:** O(256^{3})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count number of right triangles possible with a given perimeter
- Program to find the Area and Perimeter of a Semicircle
- Maximum area of rectangle possible with given perimeter
- Program for Area And Perimeter Of Rectangle
- Program to calculate area and perimeter of Trapezium
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate area and perimeter of a rhombus whose diagonals are given
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle
- Find maximum volume of a cuboid from the given perimeter and area
- Perimeter and Area of Varignon's Parallelogram
- Area of a Square | Using Side, Diagonal and Perimeter
- Find all possible triangles with XOR of sides zero
- Area of the circumcircle of any triangles with sides given
- Program to find Perimeter / Circumference of Square and Rectangle
- Program to find the Perimeter of a Regular Polygon
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Subsequence pair from given Array having all unique and all same elements respectively
- Count the number of possible triangles
- Number of possible Triangles in a Cartesian coordinate system

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.