## 验证任意一个大于4并且小于500的偶数都可以拆分成两个素数之和

2019年01月10日 ⁄ 综合 ⁄ 共 1600字 ⁄ 字号 评论关闭

```#pragma once
class CJudegeprime
{
public:
CJudegeprime(void);
~CJudegeprime(void);
private:
bool isprime(int m);
int  getallprime100(int container[]);
bool judgeisinarray(int number, int container[], int count, int start);
bool decompose(int even);
public:
void makelist();
void display();

};

#include "StdAfx.h"
#include "Judegeprime.h"
#include "math.h"

CJudegeprime::CJudegeprime(void)
{
}

CJudegeprime::~CJudegeprime(void)
{
}

bool CJudegeprime::isprime(int m)
{
int loopcount = (int)sqrt(long double(m));
for(int i=2; i<=loopcount; i++)
{
if (m % i == 0)
{
return false;
}
}
return true;
}

int CJudegeprime::getallprime100(int container[])
{
int outcome = 0;
for(int i=2; i<500; i++)
{
if (isprime(i))
{
container[outcome] = i;
outcome++;
}
}
return outcome;
}

void CJudegeprime::display()
{
int container[500];
int count = getallprime100(container);
for(int i = 0; i<count; i++)
{
printf("index=%d prime=%d\n", i, container[i]);
}
}

bool CJudegeprime::decompose(int even)
{
int container[500];
int count = getallprime100(container);
for(int i=0; i<count; i++)
{
if (container[i] < even)
{
int temp = even - container[i];
if (judgeisinarray(temp, container, count, i))
{
printf("%d = %d + %d\n", even, container[i] , temp);
return true;
}
}
else
{
break;
}
}
return false;
}

bool CJudegeprime::judgeisinarray(int number, int container[], int count, int start)
{
for(int i=start; i<count; i++)
{
if (number == container[i]) return true;
}
return false;
}

void CJudegeprime::makelist()
{
for(int i=4; i<500; i+=2)
{
decompose(i);
}
}
```

```// prime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Judegeprime.h"

int _tmain(int argc, _TCHAR* argv[])
{
CJudegeprime jp;
//jp.display();
printf("*********************\n");
jp.makelist();

getchar();
return 0;
}

```