fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main (int argc, char *argv[]) {
  5. int n = 100000;
  6. int *result = malloc((n+1) * sizeof(int));
  7. for (int i = 1; i <= n; i++) {
  8. result[i] = -1;
  9. }
  10. for (int i = 1; i <= n; i++) {
  11. int s = 0;
  12. for (long x = i; x != 1; s++) {
  13. if (x <= n && result[x] != -1) {
  14. s += result[x];
  15. break;
  16. }
  17. x = (x % 2 == 0) ? x / 2 : 3 * x + 1;
  18. }
  19. result[i] = s;
  20. }
  21. int max = 0;
  22. for (int i = 1; i <= n; i++) {
  23. if (max < result[i]) {
  24. max = result[i];
  25. }
  26. }
  27. printf("%d\n", max);
  28. }
Success #stdin #stdout 0.02s 2068KB
stdin
Standard input is empty
stdout
350