ben1996123

negative collatz

Aug 24th, 2016
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4.  
  5. #define N1 100000ULL
  6. #define N2 1000000ULL
  7. #define TABLESIZE 1610612736
  8. #define TABLENAME "table_1610612736"
  9.  
  10. int main(){
  11.     std::ofstream file("output.txt");
  12.  
  13.     char *table = new char[TABLESIZE];
  14.     for(int aa=0; aa<TABLESIZE; aa++) table[aa] = -1;
  15.     table[0] = 1;
  16.     table[4] = 2;
  17.     table[16] = 3;
  18.  
  19.     unsigned long long int s=0,m=0,l=0,n,x=0;
  20.  
  21.     //make/load the table
  22.     FILE *f;
  23.     f = fopen(TABLENAME, "r");
  24.     if(f){
  25.         fread(table, sizeof(char), TABLESIZE, f);
  26.     }
  27.     else{
  28.         for(unsigned long long int aa=1; aa<=TABLESIZE; aa++){
  29.             if(aa%1000000==0) std::cout << aa << "\n";
  30.             n=aa;
  31.             while(n > TABLESIZE || table[n-1] == -1){
  32.                 if(n%2==0) n/=2;
  33.                 else n=(3*n-1)/2;
  34.             }
  35.             table[aa-1] = table[n-1];
  36.         }
  37.         f = fopen(TABLENAME, "w+");
  38.         fputs(table, f);
  39.     }
  40.     fclose(f);
  41.  
  42.     //do the iterations
  43.     time_t t = clock();
  44.     for(unsigned long long int aa=0; aa<N1; aa++){
  45.         for(unsigned long long int ab=1; ab<=N2; ab++){
  46.             n=x+ab;
  47.  
  48.             while(n>TABLESIZE){
  49.                 if(n%2==0) n/=2;
  50.                 else n=(3*n-1)/2;
  51.             }
  52.  
  53.             if(table[n-1]==1) s++;
  54.             else if(table[n-1]==2) m++;
  55.             else if(table[n-1]==3) l++;
  56.         }
  57.  
  58.         x = N2*(aa+1);
  59.  
  60.         float totaltime = (float)(clock()-t)/60000;
  61.         float timeperloop = totaltime/(aa+1);
  62.         float remaining = timeperloop*(N1-aa-1);
  63.         std::cout << x << "\telapsed: " << totaltime << "m\tremaining: " << remaining << "m\n";
  64.         file << x << " " << s << " " << m << " " << l << "\n";
  65.     }
  66.     std::cout << s << " " << m << " " << l;
  67.     file.flush();
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment