

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Isosceles triangles with nearest perimeter using JavaScript
<h2>Almost Isosceles Triangle</h2><p>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.</p><h2>Problem</h2><p>We are required to write a JavaScript function that takes in a number which specifies the perimeter of a triangle.</p><p>Our function should find the measurement of such an almost isosceles triangle whose perimeter is nearest to the input perimeter.</p><p>For example, if the desired perimeter is 500,</p><p>Then the almost isosceles triangle with the nearest perimeter will be − [105, 104, 181]</p><h2>Example</h2><p>Following is the code −</p><p><a class="demo" href="http://tpcg.io/lwN07EQb" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notransalte">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));</pre><h2>Output</h2><pre class="result notransalte">[ 105, 104, 181 ]</pre>
- Related Questions & Answers
- All right triangles with specified perimeter in JavaScript
- Count number of right triangles possible with a given perimeter in C++
- Picking the triangle edges with maximum perimeter JavaScript
- Nearest palindrome in JavaScript
- Forming the nearest time using current time in JavaScript
- Nearest Prime to a number - JavaScript
- Finding nearest Gapful number in JavaScript
- Find number of unique triangles among given N triangles in C++
- Sum of perimeter of all the squares in a rectangle using JavaScript
- Nearest power 2 of a number - JavaScript
- Finding points nearest to origin in JavaScript
- Find the Number of Triangles After N Moves using C++
- Count of triangles with total n points with m collinear in C++
- Program to find largest perimeter triangle using Python
- Distance to nearest vowel in a string - JavaScript
Advertisements