Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Created April 25, 2017 08:25
Show Gist options
  • Save pawarvijay/c9301299eaafbb5b2fcf1b9ad9a7ea8b to your computer and use it in GitHub Desktop.
Save pawarvijay/c9301299eaafbb5b2fcf1b9ad9a7ea8b to your computer and use it in GitHub Desktop.
Easiest Pythagorean Triplet In Javascript
https://www.youtube.com/watch?v=86YAPbZmsRI&index=1&list=LLk0ldD0HZK090snvfuKTSWw
var arraylist = [3, 1, 4, 6, 5 , 12 , 13];
arraylist.forEach(function(item,index){
if(item%2 === 0){
var divided = (item/2) * (item/2)
var big = divided + 1;
var small = divided - 1;
var foundSmall = false;
var foundBig = false;
arraylist.forEach(function(item2 , index2){
if(big == item2){
foundBig = true
}
if(small == item2){
foundSmall = true
}
})
if(foundBig && foundSmall){
console.log('Hey We Found Pithagorean triplet For even item' + item + ' small ' + small + ' big ' + big)
}
}else{
var divided1 = (item * item) / 2;
var big1 = Math.ceil(divided1);
var small1 = Math.floor(divided1);
var foundSmall1 = false;
var foundBig1 = false;
arraylist.forEach(function(item2 , index2){
if(big1 == item2){
foundBig1 = true
}
if(small1 == item2){
foundSmall1 = true
}
})
if(foundBig1 && foundSmall1){
console.log('Hey We Found Pithagorean triplet For odd item' + item + ' small ' + small1 + ' big ' + big1)
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment