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)