title: Optimizing a Combination Algorithm
body: <p>I'm organizing a game tournament in a week. I began thinking about the best way to <em>mathematically</em> arrange the teams (so that they are really even, thus more competitive). So here's the data:</p>
  
  <ul>
  <li>20 players in the event</li>
  <li>Each player is assigned a skill level (high number = skilled)</li>
  <li>4 teams of 5 players each (although I'd prefer to build a algorithm that takes these as
  variables)</li>
  <li>I'm using a computer to solve the problem</li>
  </ul>
  
  <p>So, I have 20 players. I'd like to generate 4 teams with 5 players each. To do this, I'd like to generate a list of all possible team combinations. To evaluate a team combination, I:</p>
  
  <ul>
  <li>Generate a combination of teams (a match)</li>
  <li>Sum the total skill for each team based off the players in that team</li>
  <li>Compare each team to each other, the highest difference between any two teams in the match is the "tolerance" level for that match. If the tolerance level is higher than a certain cap, the match is discarded</li>
  </ul>
  
  <p>My current approach is to generate a base X number that is N digits long, where X is the number of teams I want, and N is the number of players. Then increment the base X number by 1, I'll get every possible team combination, and I can generate a list of matches that have low tolerance values.</p>
  
  <p>The problem with this, as you probably know, is for 4 teams with 20 players, that's (4-1)^20 in base 3, which is 1E12 matches to check through. (This takes a long time on my computer). Is there a mathematical way to simplify this calculation to be doable in a short period of time?</p>
  
  <p>By current method also allows for the possibility of uneven players spread across the number of teams, which is preferable. If this can't be present with a highly performant algorithm, then it's okay not to use it. </p>