Created
January 11, 2012 05:11
-
-
Save shiawuen/1593155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Combination – slooow way</title> | |
</head> | |
<body> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a number n, generate all possible sequences of numbers that add up to n, where the numbers in the | |
sequence are in non-ascending order. | |
e.g., n=4, then | |
4 | |
3,1 | |
2,2 | |
2,1,1 | |
1,1,1,1 | |
*/ | |
(function() { | |
var n = 20; | |
var slice = Array.prototype.slice; | |
var prev = +new Date(); | |
var count = 0; | |
compute(n); | |
function compute(set, variable) { | |
if (!Array.isArray(set)) { | |
set = [set]; | |
variable = 0; | |
} | |
var sLen = set.length; | |
if (variable <= set[sLen-1]) { | |
var result = set.join(','); | |
if (variable) | |
result += ',' + variable; | |
count += 1; | |
console.log(result); | |
} | |
// Terminate the loop when all value == 1 | |
if (set[0] === 1 && variable === 1) { | |
console.log('Total time spent: ' + (+new Date() - prev) + 'ms'); | |
console.log(count + ' combinations generated') | |
return; | |
} | |
var s = set[sLen-1] | |
, newSet | |
, nsLen | |
, vSet | |
, index; | |
if (s < variable) { | |
set.push(s); | |
variable -= s; | |
setTimeout(function() { | |
compute(set, variable); | |
}, 1); | |
return; | |
} | |
newSet = slice.call(set); | |
if (s === variable && s !== 1) { | |
newSet.push(s) | |
variable = 0; | |
} else if (s-variable < 1) { | |
index = indexOfBefore(set, 1); | |
newSet = slice.call(set, 0, index+1); | |
vSet = slice.call(set, index+1, sLen); | |
for (var vl = vSet.length; vl;) { | |
variable += vSet[--vl]; | |
} | |
} | |
nsLen = newSet.length; | |
newSet[nsLen-1] -= 1; | |
variable += 1; | |
if (variable > newSet[nsLen-1]) { | |
newSet.push(newSet[nsLen-1]); | |
variable = variable - newSet[nsLen-1]; | |
} else if (variable > 1) { | |
newSet.push(variable); | |
variable = 0; | |
} | |
// Hack to prevent the maximum stack call error | |
setTimeout(function() { | |
compute(newSet, variable); | |
}, 1); | |
} | |
function indexOfBefore(set, v) { | |
var index = set.indexOf(v); | |
return index === -1 | |
? set.length-1 | |
: index !== 0 | |
? index-1 // The one before the first item with value=v | |
: -1; // all value are same | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
recursion?