Powerball number generation in C…

Jeff Spencer wrote a comment about my previous posting about powerball odds. He mentioned that he could only generate 27,000 entries. As my post showed, there are several hundred million. I wrote a small C program to generate the actual powerball numbers, minus the powerball itself. In a private email, I told Jeff where he could find this file. The text file is 80MB, so if you are interested in it, let me know.

The C program is pretty simple. Essentially the key is to iterate over all the possible number combinations without repeating any numbers. This can be done by setting up a loop for each of the five possible numbers. Each subsequent number is just a loop from the previous number +1 to 55. It looks like this:

#include < stdio.h >
int main()
{
   int i, j, k, l, m;
   unsigned int count;
   count = 0;
   for (i = 1; i < 56; i++)
   {
      for (j = i+1; j < 56; j++)
      {
         for (k = j+1; k < 56; k++)
         {
            for (l = k+1; l < 56; l++)
            {
               for (m = l+1; m < 56; m++)
               {
                  printf("%u: %d %d %d %d %d\\r\\n", count, i,j,k,l,m);
                  count++;
               }
            }
          }
       }
   }
   return 0;
}

In linux, you can save the above text to a file called powerball.c. Then using gcc, compile it into an application with the command “gcc powerball.c” (leave out the quotes). Execute this and redirect the contents to a file: “./a.out > powerball_entries.txt” (leaving out the quotes again).

Leave a Reply