Prime number within 1000 of output

Source: Internet
Author: User

Remember before also wrote, at that time can not write is to read someone else's answer to write out, today again write found still won't write, it seems that the last time is not learned ah, but think about it or to come up with a not so good method, fortunately, they straighten out the idea, is their own research out of the results.

Output 1000 in the prime, then only need to take out a number each time, and then divide the number from 2 to its own, see the remainder, if the remainder has 0 of the case, then must not be prime, if all except the discovery there is no remainder of 0, then must be prime.

For example, to determine whether 7 is a prime, must see 7%[2,6] results.

Easy to find, not prime this condition is relatively easy to meet, because as long as there is a remainder of 0 can be judged, then in the program is so judged.

There is another problem, if a 7%[2,6] in the process there is a 0 of the situation, then you do not have to judge, because there is no need, you can directly break

There is one more question that is clear in the comments.

int main (void) {    /* output 1000 or less prime */int m;int n;for (n=2;n<=1000;n++) {for (M = 2; m < n; m++) {if (n% m = = 0) {break; }}/* A number is a prime or not a prime process will arrive here how to distinguish between these two situations? If it is a prime number, then must be into the loop, then must meet M<n and did not have time to execute m++, because it is the prime number of break. So m<n must be able to launch n is not prime. If a number is a prime, then it means that the inner for loop is gone and the reason for the walk is because the condition of m<n is not satisfied then the M must be equal to n at this time. So m==n must be able to launch n is prime number */if (m==n) {printf ("%d", n);}} return 1;}

  

Prime number within 1000 of output

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.