ixxra

Primes test object oriented

Apr 1st, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <list>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class PrimesBag : public list <int>
  10. {
  11. public:
  12.   bool hasDivisor (int x) {
  13.     double bound = sqrt (x);
  14.  
  15.     for (PrimesBag::iterator i = this->begin ();
  16.      (i != this->end ()) && (*i <= bound); ++i) {
  17.       if (x % *i == 0) return true;
  18.     }
  19.    
  20.     return false;
  21.   }
  22. };
  23.  
  24.  
  25. int main (int argc, char* argv[])
  26. {
  27.   PrimesBag bag;
  28.  
  29.   int top = atoi (argv[1]);
  30.  
  31.   for (int i = 2; i <= top; i++) {
  32.     if (!bag.hasDivisor (i)) bag.push_back (i);
  33.    
  34.   }
  35.  
  36.   for (PrimesBag::iterator i = bag.begin (); i != bag.end (); ++i) {
  37.     cout << *i << endl;
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment