Code for Combinations

Code for Combinations

Posers and Puzzles

Cookies help us deliver our Services. By using our Services or clicking I agree, you agree to our use of cookies. Learn More.

R
Standard memberRemoved

Joined
10 Dec 06
Moves
8528
31 Aug 16
1 edit

Does anyone know a general code that can be used to print combinations using basic programming loops? I'm completely stuck on this.

Given user parameters of n and r, I would like a simple code to explicitly print out C(n,r) in 1's and 0's perhaps using For, Do, While Loops ( or other rudimentery language structures) etc...

Example: Given the user parameters

n=4
r = 3

C(4,3) = 1110;1101;1011;0111

So... is there a program structure that will work for C(n,r) in general?

Thanks in advance for any direction on this.

Joined
18 Jan 07
Moves
12473
02 Sep 16
1 edit

Originally posted by joe shmo
Does anyone know a general code that can be used to print combinations using basic programming loops? I'm completely stuck on this.

Given user parameters of n and r, I would like a simple code to explicitly print out C(n,r) in 1's and 0's perhaps using For, Do, While Loops ( or other rudimentery language structures) etc...
I don't know whether this counts as "simple" with you, but the most convenient and IME easiest way to do this is using recursion, not simple loops.

The easiest way to implement it is probably to add a helper function which takes a prefix. Your main function then calls this with an empty prefix and lets it do all the work. (You can do without this, but then all calling code has to pass the empty prefix.) E.g.:

printcomb(n, r)
printcombhelper(n, r, "" )

printcombhelper(n, r, prefix)
If n=0 then print prefix: return
If r>0 then printcombhelper(n-1, r-1, prefix+"1" )
If r<n then printcombhelper(n-1, r, prefix+"0" )

(Apologies if that doesn't indent properly.)

It's possible to do that using only loops and hand-managed stacks, but all you'd be doing is emulate recursion anyway, and that the hard way.

R
Standard memberRemoved

Joined
10 Dec 06
Moves
8528
02 Sep 16

Originally posted by Shallow Blue
I don't know whether this counts as "simple" with you, but the most convenient and IME easiest way to do this is using recursion, not simple loops.

The easiest way to implement it is probably to add a helper function which takes a prefix. Your main function then calls this with an empty prefix and lets it do all the work. (You can do without this, bu ...[text shortened]... hand-managed stacks, but all you'd be doing is emulate recursion anyway, and that the hard way.
The problem is I'm not a programmer, and hence am very limited in translating programming languages...I'm going to be doing this in VBA, and Iv'e never used anything like this. If it wouldn't be too much trouble could you explain in some depth the purpose and output of this code?

R
Standard memberRemoved

Joined
10 Dec 06
Moves
8528
03 Sep 16

Also, maybe "print" was a bad way to present this to you. I was hoping for a usable table in Excel of real numbers 1's and 0's to turn certain computaions on or off by means of a product... I don't understand your code, but it seems as though it going to produce a string, not an array. Am I wrong on that?