Slightly faster. It's getting too silly, though. However, the step function is getting smaller due to better understanding of the problem.
// inc_c - http://perlmonks.org/?node_id=1128230 // 8.52 secs. for 16/32 count=601080390 -O2 // 3.08 secs. for 16/32 count=601080390 -O3 #include <stdlib.h> #include <stdio.h> #define N 16 // number of elements wanted #define M 32 // place static int place[N+1]; static int count = 0; int step(void) { int *p = place; for(int i = 0; i < N; *p++ = i++ ) { if(*p < p[1] - 1) { ++*p; return 1; } } return 0; } int main(int argc, char **argv) { int i; int more = 1; for(i = 0; i < N; i++) place[i] = i; place[N] = M; while( more ) { //for(i = 0; i < N; i++) printf(" %d", place[i]); //putchar('\n'); count++; more = step(); } printf("\ncount %d\n", count); exit(0); }
Thanks for the opportunity to scrape some rust off my C skills :)
In reply to Re^7: Odometer pattern iterator (in C).
by Anonymous Monk
in thread Odometer pattern iterator (in C). (Updated.)
by BrowserUk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |