C-language code Filtering for prime numbers and general search, filtering for prime numbers

Source: Internet
Author: User

C-language code Filtering for prime numbers and general search, filtering for prime numbers

Prime Number: Apart from itself, the number that cannot be divisible by other integers is called a prime number.

C code (common)
# Include
 
  
# Include
  
   
# Include
   
    
# Define N 100 // common method int main () {int I, j; for (I = 2; I <N; I ++) {for (j = 2; j <= sqrt (I); j ++) {if (I % j = 0) {break ;}} if (j> sqrt (I )) {printf ("% d \ n", I) ;}// return 0; system ("pause ");}
   
  
 

Limit Method

# Include
 
  
# Include
  
   
# Include
   
    
# Define N 100 // The number of queries for filtering // The specific screening method is to first sort n natural numbers in order. // 1 is not a prime number or a combination. // The second number 2 is the prime number, and all the numbers that can be divisible by 2 after 2 are excluded. // The first undivided number behind 2 is 3, leaving 3, and then dividing all the numbers behind 3 that can be divisible by 3. // 3 The first unallocated number after the end is 5, leaving 5, and then dividing all the numbers that can be divisible by 5. // As a result, all the groups not greater than N will be screened out, leaving all the prime numbers not greater than N. Int main () {int num = 0; int flag [N + 1]; int I, j; for (I = 2; I <= N; I ++) {flag [I] = 1; // initialization, indicating that there is no number division} for (I = 2; I * I <= N; I ++) {if (flag [I] = 1) {for (j = 2; I * j <= N; j ++) {flag [I * j] = 0; // calculate the multiple of I }}for (I = 0; I <= N; I ++) {if (flag [I] = 1) {printf ("% 4d", I); num ++; if (num % 10 = 0) {printf ("\ n ");}}} printf ("\ n"); system ("pause ");}
   
  
 

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.