Created
February 24, 2013 07:57
-
-
Save anonymous/5023058 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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