素数缓冲求法,可重用类
2004-08-21 15:39
351 查看
.S0 {
color: #808080;
}
.S2 {
font-family: Comic Sans MS;
color: #007F00;
font-size: 10pt;
}
.S4 {
color: #007F7F;
}
.S5 {
font-weight: bold;
color: #00007F;
}
.S6 {
color: #7F007F;
}
.S9 {
color: #7F7F00;
}
.S10 {
font-weight: bold;
color: #000000;
}
span {
font-family: 新宋体;
font-size: 10pt;
}
#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;
}
color: #808080;
}
.S2 {
font-family: Comic Sans MS;
color: #007F00;
font-size: 10pt;
}
.S4 {
color: #007F7F;
}
.S5 {
font-weight: bold;
color: #00007F;
}
.S6 {
color: #7F007F;
}
.S9 {
color: #7F7F00;
}
.S10 {
font-weight: bold;
color: #000000;
}
span {
font-family: 新宋体;
font-size: 10pt;
}
#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;
}
相关文章推荐
- 素数缓冲求法,可重用类
- 素数缓冲求法,可重用类
- 孪生素数问题--暴露弱点,怎么重用,让相邻两个相减
- 输出1~100之内的素数及素数个数简单程序
- 素数打表(两种方法)
- 求素数方法
- 00-自测2. 素数对猜想 (20)
- 趣味素数-素数 java
- 快速查找素数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数。 输入 给出一个正整数数N(N<=
- 算法总结:判断一个数是否为素数
- 重复造轮子之RSA算法(一) 大素数生成
- [算法练习]找出40000以内的素数
- 【模板】线性筛素数
- 天勤OJ 题目1135: 素数
- iOS UITableViewCell重用问题
- 素数求解
- OJ 2691: 输出一定范围内的素数
- 素数打表
- Java学习之处理流中的缓冲流Buffered--2015-3-2
- HDU 4548 美素数