Programming growth Diary--regain C language

Source: Internet
Author: User

Life always needs an opportunity to change, in fact, rather than an opportunity, but also to give themselves a chance.

Regain the C language, I decided to give myself a chance, do not know how many years later I look at this blog when it will be how the mood, will not remember the current as a junior dog I finally want to say goodbye to nothing, no, the decadent and not know the university life, I do not want to say is for the ideal and struggle, Not just for the sake of China's rise, but for myself, that's all.

Thanks to bit, give me a chance to change.

Start from scratch and learn C language.


--------------------------------------Sensational Split Line-------------------------------------------


Find Prime Code

#include <stdio.h>//100~200 the prime number int main () {int i=0; int j=0; for (i=100;i<200;i++) {for (j=2;j<= (I/2); j    + +) {if (i%j==0) break;    } if (i%j!=0) printf ("is the number of primes:%d \ n", i); } return 0;}

This is the first code I wrote, after the teacher explained, found that it can actually be optimized:

#include <stdio.h>//100~200 the prime number int main () {int i=0; int j=0; for (i=101;i<200;i+=2)//minus even, reducing the number of cycles {for (j=    3;j<= (I/2); j+=2) {if (i%j==0) break;    } if (i%j!=0) printf ("is the number of primes:%d \ n", i); } return 0;}

Here, there are also areas to note:

The method of seeking a prime number greater than 2 is not only N/2, but can also be n-1; or sqrt (n);

P.S: Remember to add #include<math.h> to the header file when using sqrt


This article from "10954937" blog, declined reprint!

Programming growth Diary--regain C language

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.