【C++】设计算法求1000以内的质数数量

题目:

设计算法求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);
}

 

猜你喜欢

转载自blog.csdn.net/Mitchell_Donovan/article/details/114808077