前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1013. 数素数 (20)

1013. 数素数 (20)

作者头像
AI那点小事
发布2020-04-20 14:37:24
3010
发布2020-04-20 14:37:24
举报
文章被收录于专栏:AI那点小事AI那点小事

令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。

输入格式:

输入在一行中给出M和N,其间以空格分隔。

输出格式:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入样例: 5 27 输出样例: 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103

代码语言:javascript
复制
#include<cmath>
#include<iostream>
using namespace std;

bool isPrime(int a)
{
    if(a==1 || a==0)
        return false;
    if(a==2)
        return true;
    if(a%2 == 0)
        return false;
    int temp = (int)sqrt((double)a);
    for(int i = 3;i<=temp;i++)
        if(a%i ==0 )
            return false;
    return true;
}

int main()
{
    int left,right;
    cin>>left>>right;
    int i = 1;
    int count = 0;//计数素数
    while(1)
    {
        i++;
        if(isPrime(i))
            count++;
        if(count==left)
            break;
    }
    while(count<right)
    {
        cout<<i;
        if((count-left)%10!=9)
            cout<<" ";
        i++;
        while(!isPrime(i))
            i++;
        count++;
        if((count-left)%10==0)
            cout<<endl;
    }
    cout<<i;

    system("pause");
    return 0;
}
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档