I have a recursive function that produces all combinations of length k from the set n. Commonly referred to as "nCk" ("n selects k"). I want to get rid of it with rather large values (56C22), which will produce 2 142 582 442 263 900 results. Due to implementation restrictions (I have to use VBScript and cannot stay on the computer for longer than I was at work), I can’t let it work until it is complete in one go. Thus, I would like to periodically save the current state of the function and resume it later ... but I cannot figure out how to do this. Recursion is tinkering with my ability to think it logically.
I reviewed the proposed solutions here on SO and otherwise searched for “resuming a recursive function” and the like, but to no avail. I would appreciate some pointers (NOT a pun for programming) to get me on the right track.
The actual algorithm (pseudo-code in order) is preferable to a long explanation that does not include the actual code. If you want to actually encode it, I am most familiar with C, C ++, Pascal, VB, JavaScript and VBScript (and, as indicated, works with VBScript at the moment).
Here is my recursive function:
function nCk(aSet, iSetIndex, aSubset, iSubsetIndex)
'Found result
if (iSubsetIndex > ubound(aSubset)) then
log "output.txt", join(aSubset), 1, false
exit function
end if
'No more characters available
if (iSetIndex >= ubound(aSet) + 1) then
exit function
end if
'With aSet[iSetIndex]
aSubset(iSubsetIndex) = aSet(iSetIndex)
nCk aSet, iSetIndex + 1, aSubset, iSubsetIndex + 1
'Without
nCk aSet, iSetIndex + 1, aSubset, iSubsetIndex
end function 'nCk
FYI: I am 50 years old; this is not homework.
Brian source
share