Advertisement
kcku

00100 - The 3n + 1 problem

Nov 6th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <cstdio>
  2. #include <map>
  3. using namespace std;
  4.  
  5. map<int, int> table;
  6.  
  7. int calc(int n) {
  8.     int m = table[n];
  9.     if(m) return m;
  10.     return table[n] = (n&1)?2+calc((3*n+1)>>1):1+calc(n>>1);
  11. }
  12.  
  13. int main() {
  14.     table[1] = 1;
  15.     for(int x, y; ~scanf("%d%d", &x, &y);) {
  16.         printf("%d %d ", x, y);
  17.         if(x > y) {
  18.             int t = x; x = y; y = t;
  19.         }
  20.         int ans = 0;
  21.         for(int i = x; i <= y; i++) {
  22.             int l = calc(i);
  23.             if(l > ans) ans = l;
  24.         }
  25.         printf("%d\n", ans);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement