## Featured Replies in this Discussion

- by Salem
- by Salem> for(int i=2; i<=((int)sqrt((double)n)); i++) And it would be so much quicker if you didn't call sqrt() on every iteration of the loop! n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against. Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3. Also, if you start at 3, then you can do…
- by hollystyles
- by ~s.o.s~... If the developers didnt care for speed then u wouldn't be conviniently using your favourite OS u are usign right now without atlest swearing atleast 100 times a day regarding the slow loading times. Dont forget that this is a C/ C++ forum and speed is one of the major merits of this language and bcoz of which it is used to make real time softwares. Though optimization should be last on ur list…
- by LernerTo "optimize" it a little further without getting too technical you could keep track of each prime along the way. Then to check if current number is prime just check to see if previous primes up to the square root of the current number are a factor rather than checking all odd numbers less than square root of current number. If none of the previous primes is a factor, then the current number is a…

Here you go. Added for loop in place of while, and commented out the i++ at end of while loop.

Also added int variable col, to print a newline every ten numbers printed to standard out.

```
#include<stdio.h>
#include<conio.h>
int main(int argc, char *argv[])
{
int count,i=1;
int a;
int col=0;
//while(i<=500)
for(i=1; i<501; i++)
{
count=0;
a=1;
while(a<=i)
{
if(i%a==0)
count++;
a++;
}
if(count==2){
printf("%d\t",i);
col++;
if(col%10==0)
printf("\n");
}
//i++;
}
system("PAUSE");
return 0;
}
```

Friends i have little bit problem in making a c language program.

I have to make a program which prints prime numbers from 1 to 500.

I make this program but with while loop then i was told to make it using for loop, which i tried but failed.

So kindly help me. Thanks

from,

john(pakistan)

```
int flag=1;
for(int i=2;i<500;i++) //since 1 and 500 are not prime
{
flag=1;
for(int j=2;j<i/2;j++) //since a no. cannot be divisible by a
{ //no. greater than its half.
if(i%j==0) //if i is divisible by any no. between 2 & i/2
{
flag=0;
break;
}
}
if(flag==1)
printf("\n",i);
}
```

Ok here's my best shot. The key rule for a prime number is:

It has precisely two positive integer factors.

So the neatest solution I can think of counts those and quits the for loop as soon as it's found more than 2 or reached half way to n.

Can anyone better it?

```
#include <iostream>
using namespace std;
static const int MAX = 500;
static const int MIN = 0;
static const int MAXCOL = 10;
bool isPrime(int r);
int main(int argc, char *argv[])
{
int col = 0;
int n;
for(n=MIN; n<=MAX; n++)
if(isPrime(n)){
cout << n << '\t';
col++;
if(col == MAXCOL){
cout << '\n';
col = 0;
}
}
system("PAUSE");
return 0;
}
bool isPrime(int n)
{
if((n == 0)||(n == 1))
return false;
int factors = 2;
for(int i=2; i<=((int)n/2); i++){
if(n%i == 0){
factors++;
break;
}
}
if(factors == 2)
return true;
return false;
}
```

Ah it's just occured to me what Salem meant by square root !

A revised edition:

```
#include <iostream>
#include <cmath>
using namespace std;
static const int MAX = 500;
static const int MIN = 0;
static const int MAXCOL = 10;
bool isPrime(int r);
int main(int argc, char *argv[])
{
int col = 0;
int n;
for(n=MIN; n<=MAX; n++)
if(isPrime(n)){
cout << n << '\t';
col++;
if(col == MAXCOL){
cout << '\n';
col = 0;
}
}
system("PAUSE");
return 0;
}
bool isPrime(int n)
{
if((n == 0)||(n == 1))
return false;
int factors = 2;
for(int i=2; i<=((int)sqrt((double)n)); i++){
if(n%i == 0){
factors++;
break;
}
}
if(factors == 2)
return true;
return false;
}
```

Now if only I could make i++ step in primes!

>can anyone better it?

Yes, there are more faster prime number finding Al-Gore-it-hims out there.

http://en.wikipedia.org/wiki/Sieve_of_Atkin

Note I said faster as opposed to efficient. An efficient prime number list finding strategy would be rather elusive.

Also, I'm sure you can think of a much better way to pause the program than using `system("PAUSE");`

?

:lol:

> for(int i=2; i<=((int)sqrt((double)n)); i++)

And it would be so much quicker if you didn't call sqrt() on every iteration of the loop!

n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against.

Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3.

Also, if you start at 3, then you can do i+=2 to only check all the odd numbers from there on.