Quotes help make search much faster. Example: "Practice Makes Perfect"

Tuesday, January 22, 2013

Math Problems: Distinct Possible Sums



Question:
Given an array containing consecutive numbers from 1 to 16, how many sets of 3-number combinations exist where the sum of the three numbers is unique. The numbers in each set also have to be unique. For example, (1,1,16) is not allowed since 1 is repeated.

Answer: 40
Solved using the script below

console.log("*******************************");
var somearray = [];
for(var i=5; i<=50; i+=3){
//console.log(i);
somearray.push(i);
}
console.log(somearray);
var count=0;
var sumarray=[];
for(var i=0; i<somearray.length; i++){
for(var j=0; j<somearray.length; j++){
for(var k=0; k<somearray.length; k++){
if(i!==j && j!==k && k!==i){
var sum = somearray[i]+somearray[j]+somearray[k];
if(sumarray.indexOf(sum)<0){
sumarray.push(sum);
console.log(somearray[i]+" "+somearray[j]+" "+somearray[k]+" "+sum);
count++;
}
}
}
}
}
console.log("count is "+ count);

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts