描述:用筛法求之N内的素数
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = i;
}
for (int i = 2; i < sqrt(n); i++)
for (int j = i + 1; j < n; j++)
if (arr[i] != 0 && arr[j] != 0 && arr[j] % arr[i] == 0)
{
arr[j] = 0;
}
for (int i = 2; i < n; i++)
{
if(arr[i]!=0)
{
cout << arr[i] << endl;
}
}
delete arr;
return 0;
}