Exercise 4-11 statistics primes and summed (20point (s)). C

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

Problem with the first code:
Here Insert Picture Description
Here Insert Picture Description
Correct code:
Here Insert Picture Description

//   Date:2020/3/18
//   Author:xiezhg5
#include <stdio.h>
#include <math.h>
int main(void)
{
	int k;
	int i;
	int j;
	int count=0;
	int sum=0;
	int m,n;
	scanf("%d %d",&m,&n);
	for(i=m;i<=n;i++)
	{
		k=sqrt(i);
		for(j=2;j<=k;j++)
			if(i%j==0)
			{
				break;
			}
			if(j>=k+1)
			{
				if(i==1)
				count=count;
				else
				{
					sum=sum+i;
				    count++;
		    	}
			}
	}
	printf("%d %d\n",count,sum);
	return 0;
}

The reference code is as follows:

#include <stdio.h>
#include <stdio.h>
void main()
{
	int m,k,i,n=0;
	for(m=101;m<=200;m=m+2)
	{
		k=sqrt(m);
		for(i=2;i<=k;i++)
		    if(m%i==0)
		       break;
		if(i>=k+1)
		{
			print("%d",m);
			n=n+1;
		}
		if(n%10==0)
		    printf("\n");    
	}
	printf("\n"); 
}
Published 65 original articles · won praise 28 · views 1739

Guess you like

Origin blog.csdn.net/qq_45645641/article/details/104948401