## A random Pythagorean triplet

#### <!-- // Copyright 2001 L.N. Hammer // Generates a random Pythagorean triplet based on // two parameters that are positive integers: // step is separation of the second and third numbers of the triplet. // iter is the iter^th triplet with this separation. // Currently, these are geometric distributions, because for step, // 1 and 2 are the most interesting values. var step = Math.floor( 2/Math.random() - 1 ); var iter = Math.floor( 5/Math.random() - 4 ); var temp; triplet = new Array(); if ( (step % 2) == 1) { triplet = step * (2*iter+1); triplet = 2 * step * iter * (iter + 1); triplet = triplet + step; } else { temp = step/2; if (iter == 1) iter++ ; // to avoid triplet=0 triplet = temp * 2 * iter; triplet = temp * (iter*iter - 1); triplet = triplet + step; }; document.write(triplet, "<sup>2<\/sup> + ", triplet, "<sup>2<\/sup> = ", triplet, "<sup>2<\/sup>"); //-->

By the Pythagorean theorem, a triangle with sides the lengths shown above (if you have JavaScript) is a right triangle. Neat, huh? Here's how it's done. Reload the page for another.

© 2001-2014 L. N. Hammer, last updated 16 May 2001. A link home.