collatz.c 401 B

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