[PTA] 7-33 prime statistics and summed (20 minutes)

This problem requires the statistics given integer number of prime numbers N and M within the interval and summing them.

Input format:
Input give two positive integers M and N (1≤M≤N≤500) in a row.

Output format:
sequentially outputting the number of prime numbers N M and the interval in a row and separated by a space between them and digital.

Sample input:
1031

Sample output:
7143

#include<stdio.h>
int judge(int n);
int main()
{

    int m,n,num,i=0,sum=0;
    scanf("%d %d",&m,&n);
    for(num=m;num<=n;num++){
        if(judge(num)){
            i++;
            sum=sum+num;
        }
    }
    printf("%d %d",i,sum);
}

//很重要的找素数函数
int judge(int x)
{
    int i;
    for(i=2;i<x;i++)
        if(x%i==0) break;
        if(i==x) return 1;
        else  return 0;
}

Knowledge: find prime function

Published 48 original articles · won praise 0 · Views 309

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105389954