Advertisement
bartekltg

randprim

Sep 22nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <stdint.h>
  6. #include <random>
  7. #include <cmath>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. class test
  13. {
  14.     uint64_t N;
  15.     uint64_t sN;
  16.     uint64_t p1,p2,p3,ip;
  17.     bool b;//czy jeszcze zostały nieodczytane liczby
  18.     void generate_primes()
  19.     {
  20.  
  21.         for (uint64_t i=3; i<=sN;i+=2) {
  22.              //cout<<i<<endl;
  23.             if (tab[(i-1)/2] ) {
  24.                 for (uint64_t j=(i-2)*i; j<N ; j+=i*2 )
  25.                     tab[(j-1)/2]=false;
  26.             }
  27.         }
  28.     }
  29.  
  30.     uint64_t nextprime()
  31.     {
  32.         do {
  33.             ip++;
  34.             if (ip>=tab.size()) { //liczby sie skonczyuly
  35.                 ip=2; // mimo to dokończmy wypełnianie inta bitami z początku
  36.                 b=true;
  37.             }
  38.         }while( !tab[ip] );
  39.         return 2*ip+1;
  40.     }
  41.  
  42.     int next_bit()
  43.     {
  44.         do{
  45.             p1=p2;
  46.             p2=p3;
  47.             p3=nextprime();
  48.         }while ( p3+p1== 2*p2);   // równoważne porównanium p2-p1 ? p3-p2
  49.         //num_bits++;
  50.        // if (p3+p1> 2*p2) num_set_bits++;
  51.         return (p3+p1> 2*p2);
  52.     }
  53.  
  54. public:
  55. vector <bool> tab;
  56.      uint64_t size() {return N;}
  57.     bool isprime(uint64_t n)
  58.     {
  59.         if (n%2==0) return (2==n);
  60.         else return tab[(n-1)/2];
  61.     }
  62.  
  63.     test(uint64_t n): N(n), sN(sqrt(N)+330) , tab(n/2+1,true), ip(0),b(false)
  64.     {
  65.         generate_primes();
  66.         //cout<<"1.1"<<endl;
  67.         next_bit(); next_bit(); next_bit();//przeskoczyc co najmnien 3 pierwsze
  68.     }
  69.     bool empty()
  70.         { return b; }
  71.  
  72.     uint32_t get()
  73.     {
  74.         uint32_t res=0;
  75.         for (int i=0;i<32;i++)
  76.         {
  77.             res = res*2 + next_bit();
  78.         }
  79.         return res;
  80.     }
  81.  
  82.  
  83. };
  84.  
  85.  
  86. int main()
  87. {
  88.  
  89.  
  90.     test gen(10000000000ll);
  91.  
  92.   //  uint64_t licznik=0;
  93.  
  94.   /*  for (uint64_t i=0; i!=gen.tab.size();++i)
  95.     {
  96.         if (gen.tab[i]) licznik++;
  97.  
  98.     }*/
  99.  
  100.     while (!gen.empty())
  101.     {
  102.         uint64_t x=gen.get();
  103.         cout.write((char*)&x,4);
  104.  
  105.     }
  106.    // cout<<licznik<<endl;
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement