"C Language" writing function implementation: 100-200 prime number of output __ function

Source: Internet
Author: User
Tags square root

In this, you first need to understand how prime is obtained, you can divide yourself by 2 to your previous position, according to the remainder of the situation to determine whether a prime or composite.

Optimization:

(1) Starting from 101, each time add 2, so as to improve the efficiency of one times.

(2) divided by its own square root can also be.


#include <stdio.h> #include <math.h> #include <stdlib.h> void prime () {   
 int num = 100;
    int k = 0;

    int i = 2;     for  (num = 101; num < 201; num++)   

  {        k = num / 2;         for  (;  i < num; i++)          {             if  (num % i != 0)    //all integers not--> primes, output, start to judge next number              {         
       printf ("%d    ",  num);                 break;             }              else       //can be divisible by a number--> composite numbers, Then this number can't be Sue lost, start judging the next number             {  
              break;             }       

&NBSP;&NBSP}    &nbsp}} int main () {    prime ();
    system ("pause");
    return 0; }




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.