素数之和

题目来自:网络

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

posted @ 2013-12-19 09:32  ediszhao  阅读(444)  评论(0编辑  收藏  举报