Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. long int F[1000000];
  6. long int time = 1;
  7. long int num;
  8. bool check = false;
  9.  
  10. long int calculateCollatz (long int n)
  11. {
  12.     if (check==false)
  13.     {
  14.         num = n;
  15.         check = true;
  16.     }
  17.     if (n==1)
  18.     {
  19.         time++;
  20.         F[num] = time;
  21.         time = 1;
  22.         check = false;
  23.         return F[num];
  24.     }
  25.     else if (F[num]!=-1)
  26.     {
  27.         return F[num];
  28.     }
  29.     else
  30.     {
  31.         time++;
  32.         if (n%2==0)
  33.         {
  34.             return calculateCollatz(n/2);
  35.         }
  36.         else
  37.         {
  38.             return calculateCollatz(n*3+1);
  39.         }
  40.     }
  41. }
  42. int main(){
  43.     for (long int i = 0; i<1000000; i++)
  44.     {
  45.         F[i] = -1;
  46.     }
  47.     for (long int j = 1; j<1000000; j++)
  48.     {
  49.         calculateCollatz(j);
  50.     }
  51.     long int maxVal = F[0];
  52.     long int result;
  53.     for (long int k = 0; k<1000000; k++)
  54.     {
  55.         if (F[k]>maxVal)
  56.         {
  57.             maxVal = F[k];
  58.             result = k;
  59.         }
  60.     }
  61.     cout << result;
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement