Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to have my program loop over all possible combinations of something like 5c1,5c2,5c3 etc and calculate some things within the loop.

Something like 5c1 -> 1,0,0,0,0 0,1,0,0,0 0,0,1,0,0 0,0,0,1,0 0,0,0,0,1 (5 ways)

5c2 -> 1,2,0,0,0 1,0,2,0,0 1,0,0,2,0 1,0,0,0,2 2,1,0,0,0 0,1,2,0,0 0,1,0,2,0 0,1,0,0,2 2,0,1,0,0 0,2,1,0,0 0,0,1,2,0 0,0,1,0,2 2,0,0,1,0 0,2,0,1,0 0,0,2,1,0 0,0,0,1,2 2,0,0,0,1 0,2,0,0,1 0,0,2,0,1 0,0,0,2,1 (20 ways) but 1,2,0,0,0 = 2,1,0,0,0 so we get 10 ways

5c3 -> 1,2,3,0,0 0,2,3,1,0 0,2,3,0,1 etc... simplifies 10 ways

so I'm trying to loop to find all the combinations for a general nCr-> n!/[r!*(n-r)!)

I'm stuck on how to do this in code. I'm using Xcode, objective c. I see there is an enumeration method, but it appears to be something different. Any pointers would be greatly appreciated.

share|improve this question
You need recursive method for this. check this one – Anoop Vaidya Apr 28 at 10:02

1 Answer

I have written a class in C# to handle common functions for working with the binomial coefficient which is categorized by the formula n! / (n! * (n-k)!). It performs the following tasks:

  1. Outputs all the K-indexes in a nice format for any N choose K to a file. The K-indexes can be substituted with more descriptive strings or letters.

  2. Converts the K-indexes to the proper lexicographic index or rank of an entry in the sorted binomial coefficient table. This technique is much faster than older published techniques that rely on iteration. It does this by using a mathematical property inherent in Pascal's Triangle and is very efficient compared to iterating over the set.

  3. Converts the index in a sorted binomial coefficient table to the corresponding K-indexes. I believe it is also faster than older iterative solutions.

  4. Uses Mark Dominus method to calculate the binomial coefficient, which is much less likely to overflow and works with larger numbers.

  5. The class is written in .NET C# and provides a way to manage the objects related to the problem (if any) by using a generic list. The constructor of this class takes a bool value called InitTable that when true will create a generic list to hold the objects to be managed. If this value is false, then it will not create the table. The table does not need to be created in order to use the 4 above methods. Accessor methods are provided to access the table.

  6. There is an associated test class which shows how to use the class and its methods. It has been extensively tested with 2 cases and there are no known bugs.

To read about this class and download the code, see Tablizing The Binomial Coeffieicent.

The following tested code will iterate through each unique combination:

public void Test10Choose5()
{
   String S;
   int Loop;
   int N = 10;  // Total number of elements in the set.
   int K = 5;  // Total number of elements in each group.
   // Create the bin coeff object required to get all
   // the combos for this N choose K combination.
   BinCoeff<int> BC = new BinCoeff<int>(N, K, false);
   int NumCombos = BinCoeff<int>.GetBinCoeff(N, K);
   // The Kindexes array specifies the indexes for a lexigraphic element.
   int[] KIndexes = new int[K];
   StringBuilder SB = new StringBuilder();
   // Loop thru all the combinations for this N choose K case.
   for (int Combo = 0; Combo < NumCombos; Combo++)
   {
      // Get the k-indexes for this combination.  
      BC.GetKIndexes(Combo, KIndexes);
      // Verify that the Kindexes returned can be used to retrive the
      // rank or lexigraphic order of the KIndexes in the table.
      int Val = BC.GetIndex(true, KIndexes);
      if (Val != Combo)
      {
         S = "Val of " + Val.ToString() + " != Combo Value of " + Combo.ToString();
         Console.WriteLine(S);
      }
      SB.Remove(0, SB.Length);
      for (Loop = 0; Loop < K; Loop++)
      {
         SB.Append(KIndexes[Loop].ToString());
         if (Loop < K - 1)
            SB.Append(" ");
      }
      S = "KIndexes = " + SB.ToString();
      Console.WriteLine(S);
   }
}

You should be able to port this class over fairly easily to Objective C. You probably will not have to port over the generic part of the class to accomplish your goals. Depending on the number of combinations you are working with, you might need to use a bigger word size than 4 byte ints.

share|improve this answer
I'm trying to iterate over all possible hands in a 5-card video poker game. I will take a look at your info. Thanks for the help. – Cherr Skees Apr 29 at 10:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.