fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> tabPrime;
  6. bool isPrime(int n)
  7. {
  8. bool boolIsPrime = true;
  9. int i = 0;
  10. while (boolIsPrime && tabPrime.at(i) * tabPrime.at(i) <= n)
  11. {
  12. if (n % tabPrime.at(i) == 0)
  13. boolIsPrime = false;
  14. i++;
  15. }
  16.  
  17. if(boolIsPrime)
  18. tabPrime.push_back(n);
  19.  
  20. return boolIsPrime;
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26. int numberWanted = 50;
  27. tabPrime.push_back(2);
  28. tabPrime.push_back(3);
  29. for(int i = 5; i < numberWanted; i++) {
  30. if (isPrime(i)) cout << i << endl;
  31. }
  32.  
  33. cout << "There is" << tabPrime.size() << "primes numbers from 2 to" << numberWanted << endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
5
7
11
13
17
19
23
29
31
37
41
43
47
There is15primes numbers from 2 to50