SHARE
TWEET

Untitled

a guest Jun 19th, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cmath>
  3. #include <list>
  4.  
  5. #define lim 20
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.     std::list<int>::iterator p;
  10.     std::list<int> primes;
  11.     bool s;
  12.     int n;
  13.  
  14.     primes.push_back(2);
  15.     primes.push_back(3);
  16.  
  17.     for (n = primes.back() + 1; n < lim; n++) {
  18.         for (p = primes.begin(), s = true; p != primes.end() && *p <= sqrt(n) && s; ++p) if (!(n % *p)) s = false;
  19.         if (s) primes.push_back(n);
  20.     }
  21.  
  22.     for (p = primes.begin(); p != primes.end(); ++p)
  23.         std::cout << *p << std::endl;
  24.  
  25.     return 0;
  26. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top