Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi guys


Here is a c program and I need your help!


For finding all prime Numbers between 100 to 200:

Procedure is as follows:
C++
#include<stdio.h>
#include<math.h>
void main()
{
  int m,k,i,n=0;
  for(m=101;m<=200;m=m+2)
   {
     k=sqrt(m);
     for(i=2;i<=k;i++)
        if(m%i==0)  break;
     if(i>=k+1)
        {
          printf("%d",m);
          n=n+1;
        }
     if(n%10==0)
        printf("\n");
    }
  printf("\n");
}

my question is:Program line 6, why m = m + 2, rather than m++? Why not judgment the value of m one by one?


I hope you coule help me !


Thanks!
Posted
Updated 22-Oct-13 19:58pm
v2
Comments
nv3 23-Oct-13 2:00am    
It wouldn't make sense to test even numbers for being prime, does it! That is why the loop progresses in increments of two.

This is a mathematical question rather a C programming question.

m is incremented by 2 because, every prime number is an odd number except number 2. For example take a loot at the prime numbers sequence below (Note that 1 is not considered prime):

2, 3, 5, 7, 11, ....

You can clearly see that all the prime numbers are odd expect 2, which is even.

So what advantage is there if you increment m by 1 ? Answer is None.
 
Share this answer
 
See this:http://en.wikipedia.org/wiki/Prime_number[^]

It states that:
Quote:
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because only 1 and 5 evenly divide it, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6

Note: The only even prime number is 2.

Regards..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900