Almost Isosceles Triangle
An Almost Isosceles Integer Triangle is a triangle that all its side lengths are integers and also, two sides are almost equal, being their absolute difference 1 unit of length.
Problem
We are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.
Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.
For example, if the desired perimeter is 500,
Then the almost isosceles triangle with the nearest perimeter will be − [105, 104, 181]
Example
Following is the code −
const perimeter = 500; const almostIsosceles = (perimeter = 0) => { let a = perimeter; for(; a > 0; a--){ for(let b = perimeter; b > 0; b--){ for(let c = perimeter; c > 0; c--){ if(a + b + c > perimeter || a !== b + 1 || (Math.pow(a, 3) - Math.pow(b, 3) !== Math.pow(c, 2))){ continue; }; return [a, b, c]; }; }; }; return []; }; console.log(almostIsosceles(perimeter));
Output
[ 105, 104, 181 ]