Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Created December 1, 2011 23:47
Show Gist options
  • Save freddiefujiwara/1420760 to your computer and use it in GitHub Desktop.
Save freddiefujiwara/1420760 to your computer and use it in GitHub Desktop.
Triangable max
var isTriangable = function(a,b,c){
var max = Math.max.apply(null,[a,b,c]);
var tmp = $([a,b,c]).filter(function(i,v){
return v != max;
});
if(2 != tmp.length){
return true;
}
return max < (tmp[0] + tmp[1]);
};
var input = [2,3,4,5,10];
var totals = [];
for(var i = 0; i < input.length ; i ++){
for(var j = i+1 ; j < input.length ; j++){
for(var k = j + 1 ; k < input.length ; k ++){
if(isTriangable(input[i],input[j],input[k])){
totals.push(input[i]+input[j]+input[k]);
}
}
}
}
console.log(Math.max.apply(null,totals));
@kharakawa
Copy link

Python version.

from itertools import combinations

def isTriangle(t):
  a, b, c = t
  return a < b + c and b < a + c and c < a + b

print max(filter(isTriangle, combinations([2,3,4,5,10], 3)), key=sum)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment