/* * Testing Collatz conjecture ... */ #include #include #include #define MAX (LONG_MAX/3 - 1) long stopcntr = 0L; void collatz_init(void) { stopcntr = 0L; } long collatz(long n) { if(n == 1) { return stopcntr; } else if (n >= MAX) { return -1L; } else { if (n%2 == 0) { n /= 2; } else { n = 3*n + 1; } printf("%ld\n", n); collatz(n); stopcntr++; } } int main(void) { clock_t begin, end; double time_spent; begin = clock(); collatz_init(); long n = 12346L; long i = collatz(n); printf("%ld %ld\n", n, i); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("%f sec\n", time_spent); return 0; }