其他分享
首页 > 其他分享> > 求小于正整数n的质数改良

求小于正整数n的质数改良

作者:互联网

#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; }

 

标签:std,正整数,cout,改良,int,质数,namespace,include
来源: https://www.cnblogs.com/XueQun/p/15286680.html