| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 5900f3931000cf542c50fea6 | 
					
						
							|  |  |  | title: 'Problem 39: Integer right triangles' | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:17:33 -07:00
										 |  |  | forumTopicId: 302054 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: problem-39-integer-right-triangles | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | If `p` is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | {20,48,52}, {24,45,51}, {30,40,50} | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | For which value of `p` ≤ `n`, is the number of solutions maximized? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | `intRightTriangles(500)` should return a number. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(typeof intRightTriangles(500) === 'number'); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | `intRightTriangles(500)` should return 420. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(intRightTriangles(500) == 420); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `intRightTriangles(800)` should return 720. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(intRightTriangles(800) == 720); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | `intRightTriangles(900)` should return 840. | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(intRightTriangles(900) == 840); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | `intRightTriangles(1000)` should return 840. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert(intRightTriangles(1000) == 840); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --seed--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | ## --seed-contents--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | function intRightTriangles(n) { | 
					
						
							| 
									
										
										
										
											2020-09-15 09:57:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  |   return n; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  | intRightTriangles(500); | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 19:02:05 +01:00
										 |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-02-08 16:39:32 +01:00
										 |  |  | // Original idea for this solution came from | 
					
						
							|  |  |  | // https://www.xarg.org/puzzle/project-euler/problem-39/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function intRightTriangles(n) { | 
					
						
							|  |  |  |   // store the number of triangles with a given perimeter | 
					
						
							|  |  |  |   let triangles = {}; | 
					
						
							|  |  |  |   // a is the shortest side | 
					
						
							|  |  |  |   for (let a = 3; a < n / 3; a++) | 
					
						
							|  |  |  |   // o is the opposite side and is at least as long as a | 
					
						
							|  |  |  |     for (let o = a; o < n / 2; o++) { | 
					
						
							|  |  |  |       let h = Math.sqrt(a * a + o * o); // hypotenuse | 
					
						
							|  |  |  |       let p = a + o + h;  // perimeter | 
					
						
							|  |  |  |       if ((h % 1) === 0 && p <= n) { | 
					
						
							|  |  |  |         triangles[p] = (triangles[p] || 0) + 1; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let max = 0, maxp = null; | 
					
						
							|  |  |  |   for (let p in triangles) { | 
					
						
							|  |  |  |     if (max < triangles[p]) { | 
					
						
							|  |  |  |       max = triangles[p]; | 
					
						
							| 
									
										
										
										
											2020-02-28 21:39:47 +09:00
										 |  |  |       maxp = parseInt(p); | 
					
						
							| 
									
										
										
										
											2020-02-08 16:39:32 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return maxp; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-09-30 23:01:58 +01:00
										 |  |  | ``` |