Author: Stuart Cracraft
Date: 06:55:23 07/09/04
Go up one level in this thread
On July 08, 2004 at 16:54:02, Dieter Buerssner wrote:
>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
Perfect Dieter -- I'll use this code instead. You guys are real algorithmists!
Stuart
This page took 0.01 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.