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.
38 lines
774 B
38 lines
774 B
#include <iostream>
|
|
#include <cmath>
|
|
using namespace std;
|
|
bool is_prime(int num) {
|
|
if (num < 2) {
|
|
return false;
|
|
}
|
|
|
|
for (int i = 2; i <= sqrt(num); i++) {
|
|
if (num % i == 0) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
int main() {
|
|
int n = 100; // n的值为100
|
|
|
|
int prime_count = 0; // 质数个数的计数器
|
|
|
|
for (int i = 2; i <= n; i++) {
|
|
if (is_prime(i)) {
|
|
prime_count++;
|
|
}
|
|
}
|
|
|
|
cout << log(n) << endl;
|
|
|
|
cout << "质数个数: " << prime_count << endl;
|
|
|
|
double estimated_count = (n / log(n)); // 使用质数个数定理估计质数的个数
|
|
|
|
cout << "质数个数定理估计值: " << estimated_count << endl;
|
|
|
|
return 0;
|
|
} |