Permutation Need Help To Code
Thank for your reply, first thing i wish to thank you for trying to help me out, and i have post this in few website also no one trying to help at all. For my code, what i wish to
Solution 1:
It's not that hard, I guess. It's just the standard permutation thing. You'll need to use a little recursion:
function permute(size) {
var range = getRange(size);
var result = [];
getSubPerms('', range, result);
return result;
};
function getRange(size) {
var range = [];
for (var i = 0; i < size; i++) {
range.push(i + 1);
}
return range;
}
function getSubPerms(perm, range, result) {
for (var i = 0; i < range.length; i++) {
var perm2 = perm + range[i];
if (perm2.length == range.length) {
result.push(perm2);
} else {
getSubPerms(perm2, range, result);
}
}
}
var foo = permute(4); //an array of all of your results.
alert(foo.length); //256
However, if you're only interested in the length of that, without having to generate the results, it would simply be Math.pow(size, size)
to get the length of your results.
Post a Comment for "Permutation Need Help To Code"