Talk C Chestnut Bar (eighth time: C-language Instance-prime number)

Source: Internet
Author: User
Tags square root


Everyone, crossing, start today. We talk about large couplet sci-tech fiction: C chestnuts, that is, C language examples.

Gossip Hugh,

Words return to the positive turn.

Let's talk C chestnuts together!


Crossing, they are. The last time we said is a sample of the conversion, this time we say the example is: Prime.


Primes are also called prime numbers, in natural numbers greater than 1. Suppose a positive integer except 1 and itself. cannot be divisible by other natural numbers. Then this

The number is a prime. The number of primes is much higher. So we'll just show the primes from 1 to 100 in this case.


Crossing that this example is simple. I think it's easier than that. Just walk through the numbers in the range and infer whether

Divisible by a number other than 1 and itself is possible. I specifically say prime number This example is to introduce the second method, this method can save

The time of traversal, improve the efficiency of program execution.

I used the principle in the example: if there is a positive integer n, except 1 and itself. With a square root from 2 to a positive integer n, all positive

Integer number to remove N. Suppose n is not divisible. Then the positive integer n is the prime number.

Using this method can reduce the number of traversal. thereby improving

The efficiency of the program.


Crossing, they are. No code is written in the text. The specific code in my resources, you can click the link to download the use.


Ladies and gentlemen, here's a sample of prime crossing. I want to know what the following example, and listen to tell.


Talk C Chestnut Bar (eighth time: C-language Instance-prime number)

Related Article

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.