fork download
  1. #include <stdio.h>
  2.  
  3. int cycle_length(int n);
  4. int max_cycle_length (int low, int high);
  5.  
  6. int CYCLE_LENGTHS[1000001];
  7.  
  8. int main(int argc, char *argv[]) {
  9. int low, high, max_length;
  10.  
  11. while (scanf("%d %d", &low, &high) != EOF) {
  12. int max_length = max_cycle_length(low, high);
  13. printf("%d %d %d\n", low, high, max_length);
  14. }
  15. }
  16.  
  17. int cycle_length (int n) {
  18. if (CYCLE_LENGTHS[n] != 0) {
  19. return CYCLE_LENGTHS[n];
  20. }
  21.  
  22. long long nL = n;
  23. int cycles = 1;
  24.  
  25. while (nL != 1) {
  26. if ((nL % 2) == 0) nL = nL/2;
  27. else nL = (3*nL)+1;
  28. ++cycles;
  29. }
  30.  
  31. CYCLE_LENGTHS[n] = cycles;
  32. return cycles;
  33. }
  34.  
  35. int max_cycle_length (int low, int high) {
  36. int max_length = 0;
  37.  
  38. int i;
  39. for (i = low; i <= high; ++i) {
  40. int current = cycle_length(i);
  41. if (current > max_length) max_length = current;
  42. }
  43.  
  44. return max_length;
  45. }
Success #stdin #stdout 0.87s 5584KB
stdin
1 1000000
1 10
100 200
201 210
900 1000
999999 1000000
1 1
1000000 1000000
113383 113383
1 1000000
1 1000000
stdout
1 1000000 525
1 10 20
100 200 125
201 210 89
900 1000 174
999999 1000000 259
1 1 1
1000000 1000000 153
113383 113383 248
1 1000000 525
1 1000000 525