Please turn on javascript in your browser to play chess.
Posers and Puzzles

Posers and Puzzles

  1. Subscriber joe shmo On Vacation
    Strange Egg
    31 Aug '16 00:56 / 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.
  2. 02 Sep '16 12:53 / 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.
  3. Subscriber joe shmo On Vacation
    Strange Egg
    02 Sep '16 23:09
    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?
  4. Subscriber joe shmo On Vacation
    Strange Egg
    03 Sep '16 01:08
    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?