code to write out combinations e.g. (10 choose 3)

Discussion in 'Questions (Windows Mobile)' started by Stellaferox, Feb 3, 2008.

  1. Stellaferox

    Stellaferox Active Member Licensed User

    Hi,

    Related to my last question is the following. Does anyone have code to write out every possible combination (not permutation) from a given number X taken out Y (X choose Y)?
    Any help appreciated. i just cannot seem to get the appropriate number written out.
    Thx
    Marc
     
  2. derez

    derez Expert Licensed User

    combination

    Try this link
    http://en.wikipedia.org/wiki/Mathematical_combination

    as an example - to choose 3 out of 10 you have :

    10! / (3! * (10-3)!) = 3628800/(6*5040) = 120

    (calculated by SCalculator ;) , since the only code you need is Factorial function, you can find it there.)
     
  3. derez

    derez Expert Licensed User

    combination

    Here is conceptual code, not debugged:
    sub combination(x,y)
    a = 1
    for i = 1 to x
    a = a*i
    next
    b = 1
    for i = 1 to y
    b=b*i
    next
    c=1
    for i = 1 to x-y
    c= c*i
    next
    return a/(b*c)
    end sub
     
  4. Stellaferox

    Stellaferox Active Member Licensed User

    thnx guys, but I need the code for writing out the different combinations.
     
  5. derez

    derez Expert Licensed User

    Okay, I took the challenge !
    see the attached program, both displaying and storing in a file.
    fill the a out of b and press go. you will get the number of combinations and the last one, and you can brouse up and down.:)
     
    Last edited: Feb 4, 2008
Loading...