暴力破解小于正整数n的质数
#include <iostream> 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 <= t; b = b + 2) { if (t % b == 0) c++; } if (c <= 2) { if (t == 1) cout << t + 1 << '\t'; else { cout << t << '\t'; ++h; } } c = 0; } cout << endl; cout << "素数的密度为:" << ((double)h / (double)n)*100 << "%" << endl; } int main() { GetPrimenumber(); return 0; }
浙公网安备 33010602011771号