#include <iostream> #include <iomanip> using namespace std; void GetPrimenumber() { cout<<"求小于正整数n的素数,请输入正整数:"; int n; cin >> n; int c = 0; int h = 0; cout << endl; for (int t = 1; t <= n; t = t + 2) { for (int b = 1; b <= (int)sqrt(t); ++b) { if (t % b == 0) c++; if (c >= 3) break; } if (c <= 1) { if (t == 1) cout << t + 1 << '\t'; else { cout << t << '\t'; ++h; } } c = 0; } cout << endl; cout << "素数的密度为:" << setprecision(10)<<(((double)h / (double)n)*100) << "%" << endl; } int main() { GetPrimenumber();
return 0; }