minic/test/collatz.c
raw| 1 | void *malloc(); |
| 2 | |
| 3 | main() |
| 4 | { |
| 5 | int n; |
| 6 | int nv; |
| 7 | int c; |
| 8 | int cmax; |
| 9 | int *mem; |
| 10 | |
| 11 | mem = malloc(sizeof(int) * 4000); |
| 12 | |
| 13 | cmax = 0; |
| 14 | for (nv = 1; nv < 1000; nv++) { |
| 15 | n = nv; |
| 16 | c = 0; |
| 17 | while (n != 1) { |
| 18 | if (n < nv) { |
| 19 | c = c + mem[n]; |
| 20 | break; |
| 21 | } |
| 22 | if (n & 1) |
| 23 | n = 3*n + 1; |
| 24 | else |
| 25 | n = n / 2; |
| 26 | c++; |
| 27 | } |
| 28 | mem[nv] = c; |
| 29 | if (c > cmax) |
| 30 | cmax = c; |
| 31 | } |
| 32 | printf("should print 178: %d\n", cmax); |
| 33 | } |