//sample solution
function isSumOfConsecutive2(n) {
var waysToRepresent = 0;
for(i=2;i<n;i++){
var testValue = Math.floor(n/i)-2;
if(testValue<1){ continue; };
var total = sumArray(consecutiveArray(testValue,i));
if(total==n){
waysToRepresent++;
} else if(total>n){
break;
}
}
for(i=2;i<n;i++){
var testValue = Math.floor(n/i)-1;
if(testValue<1){ continue; };
var total = sumArray(consecutiveArray(testValue,i));
if(total==n){
waysToRepresent++;
} else if(total>n){
break;
}
}
for(i=2;i<n;i++){
var testValue = Math.floor(n/i);
if(testValue<1){ continue; };
var total = sumArray(consecutiveArray(testValue,i));
if(total==n){
waysToRepresent++;
} else if(total>n){
break;
}
}
return waysToRepresent;
}
function consecutiveArray(start,size){
var someArray=[];
for(i=0;i<size;i++){
someArray.push(start+i);
}
//document.writeln(someArray," total = ");
return someArray;
}
function sumArray(someArray){
var total=0;
for(i=0; i<someArray.length; i++){
total=total+someArray[i];
}
//document.writeln(total,"<br/>");
return total;
}
No comments:
Post a Comment