素数之和

题目来自:网络

Description

对于任意给定的两个整型数N和M,你需要做的事情是求出[N,M]之间的所有素数之和。我们确保0 <= N < M <= 100。

所谓素数即:只能被1及其自身整除的数。注:1不是素数。

Input

输入的数据仅包含两个整型数N和M,N和M之间用空格间隔

Output

输出只包含一个数,即[N, M]之间的素数之和

Sample Input

1 10

Sample Output

17

//-----------------------------------------------------------------------

//这个还是比较简单的

#include <iostream>
#include <cmath>
using namespace std;
void sum_sushu(int a,int b);
bool Is_sushu(int n);
int main()
{
    int a,b;
    cin >> a >> b;
    sum_sushu(a,b);
    return 0;
}
bool Is_sushu(int n)
{
    if (n < 2)
    {
        return false;
    }
    int i,j;
    for (i = 2,j = int(sqrt(n));i<=j;i++)
    {
       // cout << j<< endl;    // Using cout to check how 'j' change
        if ((n % i) == 0)
        {
            return false;
        }
        //cout << n % i << endl;  // Using cout to check how ' n%i ' change
    }
    return true;
}
void sum_sushu(int a,int b)
{
    int sum = 0;
    for (int i = a;i <= b;i++)
    {
        if (Is_sushu(i))
        {
            sum += i;
            //cout << i << "\t";    //Using cout to check how ' i ' change
        }
    }
    cout << sum << endl;
}

//I like to cout some variables to check if they are right, and how they change

//Could you please send your codes to my e-mail ediszhao@sina.com If you have a better way to deal with it

转载于:https://www.cnblogs.com/ediszhao/p/3481491.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值