#include "bench.h" /* * Towers of Hanoi * * This calculates the number of moves required to solve a "tower of * hanoi" game with 'num' slices. It's using a recursive function to * go thru all moves required an counts them in the global variable * hanoi_moves. * * Much faster would be "moves = (2 ** num) - 1", but this is be pretty * hard to benchmark since it would only require a few cpu clocks to * calculate.... ;-) */ int hanoi_moves; void hanoi_f1(int from, int to, int num) { int buffer=0; while (buffer==from || buffer==to) buffer++; if (num>1) hanoi_f1(from, buffer, num-1); hanoi_moves++; if (num>1) hanoi_f1(buffer, to, num-1); } void test_hanoi() { timer_start(CONTEXT_LOCAL); hanoi_moves=0; hanoi_f1(0,2,24); /* 239 is a prim number. */ result_push(CONTEXT_LOCAL, hanoi_moves % 239); timer_stop(CONTEXT_LOCAL); }