2007년 12월 01일
[TDD] 에라토스테네스의 체 C++
[TDD] 에라토스테네스의 체.
"생각하는 프로그래밍"에서 index 를 하나의 데이타로 사용하는 걸를 보았음에도 불구하고, 삽질 한 번 했다가 다시 고친 버전.
이런 거 짤때마다 아직 열심히 해야겠구나 라는 생각을 하게 된다.
// CEratosthenes2.h
class CEratosthenes2 {
public:
CEratosthenes2();
void SetNums(int n);
void SetFrom(int n) { m_nFrom = n; }
void EraseCompositeNum(int n);
void GetPrimeNums(int n, std::vector<int>& rets);
std::vector<int> m_Nums;
int m_nFrom;
int m_nMax;
};
// CEratosthenes2.cpp
CEratosthenes2::CEratosthenes2() :m_nFrom(2), m_nMax(0) {}
void CEratosthenes2::SetNums(int n) {
m_Nums.clear();
m_nMax = n;
m_Nums.assign(n, 1);
}
void CEratosthenes2::EraseCompositeNum(int n) {
//for (int i = n * 2; i < m_nMax; i += n) {
for (int i = n * n; i < m_nMax; i += n) { // 최적화 1
m_Nums[i] = 0;
}
}
void CEratosthenes2::GetPrimeNums(int n, std::vector<int>& rets) {
SetNums(n);
for (int i = 2; i < m_nMax; ++i) {
if (m_Nums[i]) {
EraseCompositeNum(i);
}
}
for (int i = m_nFrom; i < m_nMax; ++i) {
if (m_Nums[i]) {
rets.push_back(i);
}
}
}
// TestEratosthenes2.cpp
struct FixtureEratosthenes2 {
CEratosthenes2 e;
std::vector<int> ret;
};
struct FixtureEratosthenes2_10 : public FixtureEratosthenes2 {
FixtureEratosthenes2_10() {
e.SetNums(10);
}
};
TEST_FIXTURE(FixtureEratosthenes2_10, SetNums) {
int expect[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
CHECK_ARRAY_EQUAL(expect, e.m_Nums, 10);
}
TEST_FIXTURE(FixtureEratosthenes2_10, EraseCompositeNum) {
e.EraseCompositeNum(2);
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int expect[] = {1, 1, 1, 1, 0, 1, 0, 1, 0, 1};
CHECK_ARRAY_EQUAL(expect, e.m_Nums, 10);
}
TEST_FIXTURE(FixtureEratosthenes2, GetPrimeNums) {
e.GetPrimeNums(10, ret);
int expect[] = {2, 3, 5, 7};
CHECK_ARRAY_EQUAL(expect, ret, 4);
}
TEST_FIXTURE(FixtureEratosthenes2, GetPrimeNums1000) {
int expect[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
e.GetPrimeNums(1000, ret);
CHECK_ARRAY_EQUAL(expect, ret, 168);
}
TEST_FIXTURE(FixtureEratosthenes2, GetPrimeNums51_100) {
int expect[] = {53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
e.SetFrom(51);
e.GetPrimeNums(100, ret);
CHECK_ARRAY_EQUAL(expect, ret, 10);
}
http://neocode.egloos.com/1017597
http://www.mathlove.co.kr/kyogu/et_show.php?code=01&seq=10
# by | 2007/12/01 12:53 | 개발 이야기 | 트랙백 | 덧글(0)




☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]