0

Hi all,

I have begun workin on getting c++ to do physics and things for me so im working on some maths functions. starting small and working up, Just did this one on prime number generation and was wondering if anyone can point out any ways it could be ,made significantly faster (excluding platform dependant optomisation) or if they can see any technical problems or coding good practise issues. I have checked the output and i think it is solid but i was wondering what you guys here thought.

So heres the code

```
//primes.cpp prime number calculator
#include <vector>
#include <limits.h>
#include <iostream>
const unsigned long c_intLimit = 1000;
using namespace std;
int main(int argc, char** argv)
{
//init vars
unsigned long result = 0;
typedef vector<unsigned long> primeList_t;
primeList_t primes;
bool isPrime;
//init primes
primes.push_back(3);
primes.push_back(4);
//for each number
for( unsigned long x = 4; x < c_intLimit ; x++ )
{
isPrime = true; //assume the number is prime
//divide by each prime
for( primeList_t::iterator iPrime = primes.begin(); iPrime != primes.end(); iPrime++ )
{
result = ( x % (*iPrime) ); //check the result of the division by each prime number
if( result == 0 )//if the number dives evenly then it is not prime
{
isPrime = false;
break;
}
else{}
}
//if the number was prime keep it
if( isPrime )
{
primes.push_back(x);
}
}
//list generated so print the results
cout<<"List of prime numbers from 1 -> "<<c_intLimit<<endl;
for( primeList_t::iterator iPrime = primes.begin(); iPrime != primes.end(); iPrime++ )
{
cout<<*iPrime<<endl;
}
cout<<"\nEnd of list"<<endl;
cin.get(); /* i know this is soo wrong and bad but im just lazy in small test appls to use proper pause code here or to pipe output to a file */
return 0;
}
```

*Edited 5 Years Ago by Kanoisa*: n/a