SHARE
TWEET

Collatz

a guest Mar 7th, 2013 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int arr[1000000]; // arr[i] = length of chain of starting with i
  6.  
  7. int main()
  8. {
  9.         long temp;
  10.         int count, max = 0, maxInd;
  11.         for(int i = 1; i < 1000000; i++){
  12.                 temp = i;
  13.                 count = 0;
  14.                 while(temp!=1){
  15.                         if(temp < i){
  16.                                 count+=arr[temp];
  17.                                 break;
  18.                         } else {
  19.                                 if(temp%2== 0) temp/=2;
  20.                                 else temp = 3*temp+1;
  21.                         }
  22.                         count++;
  23.                 }
  24.                 arr[i] = count;
  25.                 if(count > max){
  26.                         max = count;
  27.                         maxInd = i;
  28.                 }
  29.         }
  30.         cout << maxInd << endl;
  31.         return 0;
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top