04-3. Count the prime number and sum (20)

Source: Internet
Author: User

Calculate the number of prime numbers in the given integer m and n and sum them.

Input Format:

Input two positive integers m and n (1 <= m <= n <= 500) in a row ).

Output Format:

In a row, the number of prime numbers in the M and N intervals and their sum are output sequentially. Numbers are separated by spaces.

Input example:

10 31

Output example:

7 143

Note: Calculate the prime number first. Every time one is counted, add 1 to the counter and perform the sum operation.

#include "stdio.h"int main(){    int M,N;    scanf("%d %d",&M,&N);    int i=2,j=0;    int count=0,reg=0;    while(M<=N)    {        while(i<=M)        {            while(M%i==0)            {                j++;                i++;            }            i++;        }        i=2;        if(j==1)        {            reg+=M;            count++;        }        j=0;        M++;    }    printf("%d %d",count,reg);    return 0;}

04-3. Count the prime number and sum (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.