You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
400 B
30 lines
400 B
#include<iostream>
|
|
using namespace std;
|
|
|
|
|
|
//改进, 去掉偶数的判断
|
|
bool isPrime(int n)
|
|
{
|
|
if(n < 2) return false;
|
|
if(n == 2) return true;
|
|
if(n % 2 == 0) return false;
|
|
for(int i = 3; i < n; i += 2)
|
|
if(n%i == 0) return false;
|
|
return true;
|
|
}
|
|
//时间复杂度O(n/2), 速度提高一倍.
|
|
|
|
|
|
int main()
|
|
{
|
|
for(int i=0;i<100;i++)
|
|
{
|
|
if(isPrime(i))
|
|
{
|
|
cout<<i<<endl;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|