# 求解1-10000区间内的素数

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

#define MAX 100000000

int main()
{
//	int a[MAX + 1];
int *a = (int *)malloc((MAX + 1)*sizeof(int));
if(a == NULL)
{
cout << "create memory error!" <<endl;
return -1;
}
for (int i = 1; i <= MAX + 1; i++)	//初始化
a[i] = 1;

int t = 0;

clock_t start = clock();
for (int j = 2; j <= MAX; j++)
{
if (a[j])
{
for (t = 2; t * j <= MAX; t++)	//删除t*p，其中p>=2
{
a[t * j] = 0;
}
}
}
clock_t end = clock();

printf("Time is: %f\n", (double)(end - start)/CLOCKS_PER_SEC);

for (int i = 2; i <= 5000; i++)
if(a[i])
printf("%d ", i);

free(a);

return 0;
}

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;

#define MAX 100000000

int main()
{
//	int a[MAX + 1];
int *a = (int *) malloc((MAX + 1) * sizeof(int));
if (a == NULL)
{
cout << "create memory error!" << endl;
return -1;
}
for (int i = 1; i <= MAX + 1; i++) //初始化
a[i] = 1;

int t = 0;
int j;

clock_t start = clock();
for (int i = 2; i * i <= MAX; i++)
{
if (a[i])
{
for (j = i * i; j <= MAX; j += i)
a[j] = 0;
}
}
clock_t end = clock();

printf("Time is: %f\n", (double) (end - start) / CLOCKS_PER_SEC);

for (int i = 2; i <= 5000; i++)
if (a[i])
printf("%d ", i);

free(a);

return 0;
}