fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. long arr[10000];
  5. long dp(int n)
  6. {
  7. if(n==2|| n==1) return 1;
  8.  
  9. if (n >= 10000) {
  10. std::cout << n << " is really big\n";
  11. return 1 + dp((n % 2 == 0) ? n / 2 : 3 * n + 1);
  12. }
  13.  
  14. if(arr[n]) return arr[n];
  15.  
  16.  
  17.  
  18. if(n%2==0) return arr[n]=1+dp(n/2);
  19. else if(n%2==1) return arr[n]=1+dp(3*n+1);
  20. return arr[n];
  21. }
  22.  
  23. int main() {
  24.  
  25. for (int n : {999, 907}) {
  26. const long res = dp(n);
  27. std::cout << "count for " << n << " is " << res << std::endl;
  28. }
  29. }
Success #stdin #stdout 0s 3332KB
stdin
Standard input is empty
stdout
11392 is really big
count for 999 is 49
13120 is really big
count for 907 is 54