题目:
设计算法求1000以内(包含1000)的质数数量
//求1000以内的质数数量,分析算法的时间复杂度
//author:Mitchell_Donovan
//date:2021.3.2
#include <iostream>
#include<cmath>
using namespace std;
int countPrime(int n) {
int count = n - 1;
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
count--;
break;
}
}
}
return count;
}
int main() {
cout << countPrime(1000);
}