To determine the number of prime numbers in 101-200, output these primes and the number

Source: Internet
Author: User

1. The definition of prime number (prime number): A tree that is divisible only by 1 and itself

2, how to judge a prime is not primes: Use this number to remove 2 to sqrt (this number), if divisible is not prime, can not be divided into prime.

3, Code implementation

#include <stdio.h>	//Judge How many primes there are between 101-200 and the number of these primes and primes
int main ()
{
	int i, J, m, h = 0;
	for (M = <= m m++)
	{
		int leap = 1;
		for (i = 2; i*i <= m. i++)
		if (m%i = = 0)
		{
			leap = 0; break;
		}
		if (Leap)
		{
			printf ("%-4d", m);
			h++;
			if (h% = 0)
				printf ("\ n");
		}
	printf ("\ n is%d\n", h);
	return 0;
}
4, run the screenshot

5. Problems arising in the process

A, starting to put the leap=1 on the outside of the first for loop, the result is always only a number of 101, then the students told me should be placed in the first for loop inside, because each cycle will need to reset Leap 1, on the outside can not achieve this effect. Later on the Internet to see it in another solution, before the next cycle plus leap=1;

B, i*i<=m; began to refer to the online approach is j=sqrt (m+1), and then the students said open error, and this method will not, specifically I do not know what is going on.

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.