char SCCSid[] = "@(#) @(#)hanoi.c:3.3 -- 5/15/91 19:30:20"; #define other(i,j) (6-(i+j)) #include #include #include #define ITERS 200 void mov(int n, int f, int t); unsigned long iter = 0; int disk = 10; /* default number of disks */ int num[4]; long cnt; u64_t timestamp; int main(int argc, char **argv) { //~ //~ if (argc < 2) { //~ fprintf(stderr,"Usage: %s duration [disks]\n", argv[0]); //~ exit(1); //~ } //~ duration = atoi(argv[1]); printf("Test Hanoi:\n"); if(argc > 2) disk = atoi(argv[2]); num[1] = disk; timestamp = malt_get_time_us_64(); while(++iter < ITERS) mov(disk,1,3); timestamp = malt_get_time_us_64() - timestamp; printf("%ld iterations with %d disks for %lluus = %f iterations per ms\n", iter, disk, timestamp, 1000*iter/(double)timestamp); #ifdef __MALT malt_trace_finish(); #endif return 0; } void mov(int n, int f, int t){ int o; if(n == 1) { num[f]--; num[t]++; return; } o = other(f,t); mov(n-1,f,o); mov(1,f,t); mov(n-1,o,t); }