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

Source: Internet
Author: User
Tags square root


Crossing, everyone, from today onwards, we talk about the large-scale couplet science fiction: C Chestnut, that is, C language examples. Gossip Hugh,

Words return to the positive turn. Let's talk C chestnuts together!


Crossing, the last time we were talking about the example of a conversion, this time we say the example is: Prime.


Prime is also called Prime, in the natural number greater than 1, if a positive integer other than 1 and itself, can not be divisible by other natural numbers, then this

The number is a prime. The prime numbers are more, so we only show the primes from 1 to 100 in this case.


Crossing that this example is simple, I think it is relatively simple. Just walk through the number in the range to determine if it can be

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

Traverse the time, improve the program running efficiency.

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

Integer number to remove N, if n is not divisible. Then the positive integer n is the prime number. Using this method can reduce the number of traversal and thus improve

The efficiency of the program.


Crossing, the text does not write code, the detailed code put in my resources, you can click the link to download the use.


You crossing, we're going to talk about prime numbers here. 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.