Here is a recursive algorithm, it assumes a range:
function ranges(from,to,soFar){
console.log(soFar);
if(from === to){
return;
}
for(var i = from + 1;i <= to;i++){
ranges(i,to,soFar+i); // append any number that's still possible
}
}
Usage is ranges(0,7,"")
.
At each stage:
- Print the current result (atm prints the empty string too, you can filter it out)
- If there are bigger values - iterate over the bigger values and call the function recursively.