Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <list>
- #include <cstdlib>
- using namespace std;
- class PrimesBag : public list <int>
- {
- public:
- bool hasDivisor (int x) {
- double bound = sqrt (x);
- for (PrimesBag::iterator i = this->begin ();
- (i != this->end ()) && (*i <= bound); ++i) {
- if (x % *i == 0) return true;
- }
- return false;
- }
- };
- int main (int argc, char* argv[])
- {
- PrimesBag bag;
- int top = atoi (argv[1]);
- for (int i = 2; i <= top; i++) {
- if (!bag.hasDivisor (i)) bag.push_back (i);
- }
- for (PrimesBag::iterator i = bag.begin (); i != bag.end (); ++i) {
- cout << *i << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment