This program finds prime numbers.Code:#include <vector> #include <iostream> #include <cmath> using namespace std; int main(int argc, char *argv[]) { time_t start,end; time (&start); int lastTry = atoi(argv[1]); vector<int> PrimeList(1,2); vector<int>::iterator a; for (int numerator = 2; numerator <= lastTry; numerator++) { for (a = PrimeList.begin(); (a != PrimeList.end()) && (*a * *a <= numerator); a++) { if (numerator % *a) {} else {break;} } if (*a * *a > numerator) { PrimeList.push_back(numerator); cout << numerator << endl; } } PrimeList.clear(); time (&end); double dif = difftime (end,start); cout << dif << endl; return 0; }
Why is it so slow compared to a similar program that I found on the internet that did this twice as fast?
Are vectors really slow? The other program used a linked list.



LinkBack URL
About LinkBacks


