Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Permuting and Combining

Author: Dieter Buerssner

Date: 13:54:02 07/08/04

Go up one level in this thread


Actually, thinking about it again, perhaps my first code wawn't even that dumb,
compared to the rather easy

#include <stdio.h>

int main(int argc, char *argv[])
{
  unsigned long i, l, n=argc-1;

  if (n > 31) /* Or use some <limits.h> info for unsigned long */
    return 1; /* Would produce many Gigabytes of output anyway */

  l = (1UL<<n)-1;
  do
  {
    for (i=0; i<n; i++)
      if (l&(1UL<<i))
        printf("%s ", argv[i+1]);
    printf("\n");
  }
  while (l-- != 0);
  return 0;
}

The inner loop is accessing the bits of l, which will need to loop over all
bitpositions (or will need some sort of FirstOne, which probably will be much
slower, because on average, half of the bits will be set anyway). So (if we
ignore printf, which of course will need the most time here), n ifs in the inner
loop. The other code generates the combinations a bit more complicated, but
practically only needs one if(--q[0] < 0) { /* code most time not needed */ }
per iteration.

Regards,
Dieter



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.