Prime number between 100~200 (and its four optimization schemes)

Source: Internet
Author: User

The number of requests, first of all we need to know what is prime, in the problem-solving, do not rush to find ways to solve problems, but to understand the root of it, in the face of similar problems can be easily resolved

Prime Number (prime number) also known as prime number, there are unlimited. A natural number greater than 1, except 1 and itself, cannot be divisible by other natural numbers, in other words, the number except 1 and itself no longer have other factors; otherwise called composite.

The procedure is as follows:

#include <stdio.h>

#include <stdlib.h>

int main ()

{

int i;

for (i = +, I <=, i++)//can also set a range of values

{

int j = 0;

for (j = 2; J <= I-1; j + +)

{

if (i%j = = 0)

{

Break

}//If the i%j=0 description I can divide the J, jump out of the IF statement

}

if (i = = j)

{

printf ("%d", I);

}

}

System ("pause");

return 0;

}

The results are as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/79/FE/wKioL1agaOiTj2jzAAAVqokjaIw433.png "title=" primes. png "alt=" Wkiol1agaoitj2jzaaavqokjaiw433.png "/>

Optimization Scenario One:

int main ()

{

int i;

For (i = 101; I <=; i+=2) //Since 100 is not a prime number, it can start from 101

{

int j = 0;

for (j = 2; J <= I-1; j + +)

{

if (i%j = = 0)

{

Break

}//If the i%j=0 description I can divide the J, jump out of the IF statement

}

if (i = = j)

{

printf ("%d", I);

}

}

System ("pause");

return 0;

}

Optimization Scenario Two:

int main ()

{

int i;

for (i = 101; I <=; i+=2)//Since 100 is not a prime number, it can start from 101

{

int j = 0;

for (j = 3; J <= I-1; j + +)

{

if (i%j = = 0)

{

Break

}//If the i%j=0 description I can divide the J, jump out of the IF statement

}

if (i = = j)

{

printf ("%d", I);

}

}

System ("pause");

return 0;

}

Optimization Scenario Three:

int main ()

{

int i;

for (i = 101; I <=; i+=2)//Since 100 is not a prime number, it can start from 101

{

int j = 0;

for (j = 3; J <= i-1; j+=2)

{

if (i%j = = 0)

{

Break

}//If the i%j=0 description I can divide the J, jump out of the IF statement

}

if (i = = j)

{

printf ("%d", I);

}

}

System ("pause");

return 0;

}

Optimization Scheme IV:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main ()

{

int i;

for (i = 101; I <=; i+=2)//Since 100 is not a prime number, it can start from 101

{

int j = 0;

for (j = 3; J <= sqrt (i); j+=2)

{

if (i%j = = 0)

{

Break

}//If the i%j=0 description I can divide the J, jump out of the IF statement

}

if (j>sqrt (i))

{

printf ("%d", I);

}

}

System ("pause");

return 0;

}



The original purpose of writing a program may be to solve a problem or a practical problem, but, when we enter the community, you have to solve all is the actual problem, then you face may not only solve it, but to constantly optimize it, to make costs lower, shorter, more efficient, And this optimization to solve the problem of thinking, from the writing of small programs to start training.









Prime number between 100~200 (and its four optimization schemes)

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.