文逸首页 小文论坛 文逸博客 精华文章
 首页 | 新闻 | 论坛 | 博客 | 专题 | FTP | 金融 | 微博 | 图库 | MyHome | 搜索 | 登陆 | 注册 | 帮助 | 设为首页  ·在线人数: 1254
 §您的位置:文逸首页 > chinesedragon 's home > 【理论研究】专栏 > 素数缓冲求法,.
chinesedragon 的主页网址:http://wonyen.net/home.aspx?id=chinesedragon 给我留言

 § chinesedragon  的【理论研究】专栏

  作者:chinesedragon  发表时间: 2005/12/3 13:33:09    查看: 4988     评论: 2
标题: 素数缓冲求法,可重用类

素数缓冲求法,可重用类 
  #include <iostream> 
  #include <algorithm> 
  #include <vector> 
  #include <iterator> 
  
  template <typename T>
  class PrimerCache 
- { 
      typedef T   ValueType;
      typedef std::vector<ValueType> IntArray; 
      typedef typename IntArray::const_iterator IntPtr; 
   
      static IntArray mCache; 
       
  protected: 
      bool Test(ValueType num) const 
-     { 
          // make sure : half  < mCache.back() 
          // !!! IncreaseCache(half) promised this !!!        
   
          ValueType half = num / 2 + 1; 
         
          for(IntPtr i = mCache.begin(); *i < half && i != mCache.end();  ++i) 
-         { 
              if( num % (*i) == 0) 
-             { 
                   return false; 
              } 
          }         
          return true; 
      } 
   
      void IncreaseCache(ValueType num) const
-     { 
          ValueType upper = mCache.back(); 
           
          while (upper < num) 
-         { 
              upper++;             
              if( Test(upper)) 
-             { 
                  mCache.push_back(upper); 
              } 
               
          }         
      } 
   
      bool InCache(ValueType num) const
-     { 
          return std::binary_search(mCache.begin(), mCache.end(), num); 
      } 
  public: 
      PrimerCache(ValueType lowerNum = 1000)
-     { 
          IsPrimer(lowerNum); 
      } 
   
      bool IsPrimer(ValueType num) const
-     { 
          ValueType biggestPrimer = mCache.back() + 1; 
   
          if( biggestPrimer > num) 
-         { 
              return InCache(num); 
          } 
          else 
-         { 
              IncreaseCache(num); 
              return Test(num);  
          } 
      } 
      friend std::ostream& operator<<(std::ostream& out, PrimerCache const& primerCache) 
-     { 
          IntArray& cache = primerCache.mCache;
          out << "Primer Cache Has " << cache.size() << " elements, they are " << std::endl;  
          std::copy(cache.begin(), cache.end(), std::ostream_iterator<int>(out, ",")); 
          return out; 
      } 
  };
  //the one and only cache;
  template <typename T>
  typename PrimerCache<T>::IntArray PrimerCache<T>::mCache = IntArray(1, 2); 
  
  template <typename CacheType>
  void TestPrimer(CacheType& cache, int num) 
- { 
      int test = num; 
      std::cout<< test << " is primer? "<< std::boolalpha << cache.IsPrimer(test) << std::endl; 
  } 
   
   
  int main() 
- { 
      PrimerCache<int> primer; 
      int test; 
   
      TestPrimer(primer, 1); 
      TestPrimer(primer, 2); 
      TestPrimer(primer, 4); 
      TestPrimer(primer, 5); 
      TestPrimer(primer, 99); 
      TestPrimer(primer, 97); 
      TestPrimer(primer, 101); 
      TestPrimer(primer, 102); 
      TestPrimer(primer, 98); 
      TestPrimer(primer, 96); 
       
   
      std::cout << primer << std::endl; 
  
      std::cout << "primer is " << sizeof ( PrimerCache<int>) << std::endl;
  
      PrimerCache<short> p(10);
      
      TestPrimer(p, 1);
      TestPrimer(p, 2);
  
      std::cout << p << std::endl;
      return 0; 
  }
 

分享到:


 §评论: 素数缓冲求法,可重用类

  回复者:王博    回复时间:2012/7/24 22:17:03    [第1评]
要求大家点击一下查看这些不法分子!
星光大道抽奖活动
星光大道官方网站
星光大道中奖查询网
星光光大道中奖是真的吗
QQ信封


1 条评论;  每页显示 15 条评论;   1 / 1               ↑到页首




您未登陆,发表评论时请填写:用户名 密码 注册新用户  
 评论: 素数缓冲求法,可重用类
内容 (8000字以内)
 (CTRL+ENTER提交) 
  关闭窗口  
用户登陆
我要发表文章
搜 索
§chinesedragon 的网志导航
感想随笔(4)
生活休闲(0)
饮食健康(0)
自然妙趣(1)
潮流时尚(0)
游览见闻(0)
情感绿洲(6)
娱乐搞笑(4)
读图时代(3)
影音视听(4)
商业新知(0)
理论研究(6)
时事纵横(13)
社会文化(15)
文学欣赏(3)
教育学习(15)
§chinesedragon 的友情链接
关于文逸 | 小文论坛 | 文逸博客 | 文逸金融 | 精华文章网站地图 | 联系我们 | 隐私保护
 Copyright© WWW.WONYEN.NET 2003 - 2021  闽ICP备09016518号-16   本站最高 10508 人同时在线,发生时间 2005-5-17 5:09:15 
 文逸科技 制作维护