Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2013 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5023058 to your computer and use it in GitHub Desktop.
Save anonymous/5023058 to your computer and use it in GitHub Desktop.
var triplet = function(m,n) {
if(m < n)
{
console.log("M MUST BE GREATER THAN N!");
}
if((m-n) % 2 === 0)
{
console.log("Even");
}
else
{
console.log("Odd");
for(i = 1 ; i <= 1000 ; i++)
{
if(m%i ===0 && n%i === 0)
{
if (i < 2)
{
console.log("x = " + (Math.pow(m,2) - Math.pow(n,2)));
var x = (Math.pow(m,2) - Math.pow(n,2));
console.log("y = " + (2 * m * n));
var y = (2 * m * n);
console.log("z = " + (Math.pow(m,2) + Math.pow(n,2)));
var z = (Math.pow(m,2) + Math.pow(n,2));
if(Math.pow(x, 2) + Math.pow(y, 2) === Math.pow(z, 2))
{
console.log("Yes, that is a pythagorean triplet!");
if(x + y + z === 1000)
{
console.log("This is the solution to Problem 9 on ProjectEuler.net!");
confirm("CONGRATS! You found it!!");
return(x * y * z);
}
else
{
console.log("This is not the solution to Problem 9 on ProjectEuler.net");
}
}
else
{
console.log("No, that's not a pythagorean triplet!");
}
}
}
}
}
console.log("Finished");
};
triplet(20,5)
/*Only Found after a bit of testing.
Took me about 10 or 20 tries after writing the code
Certainly a lot easier than any other way!*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment