fork(2) download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. bool isPrime(int num)
  5. {
  6. // If your function is only executed from that loop above, delete this statement.
  7. if(num < 2)
  8. // Technically speaking this should be false. (I don't know if your code requires these numbers to be true.)
  9. return true;
  10.  
  11. if(!(num % 2))
  12. return false;
  13.  
  14. int sqrt_n = sqrt(num); // changed varaible name, it conflicted with sqrt()
  15. for (int i=3; i<sqrt_n; i += 2)
  16. if (!(num % i))
  17. // You can stop after you found the first divisor
  18. return false;
  19.  
  20. return true;
  21. }
  22.  
  23. int main() {
  24. for (int i = 0; i < 100; ++i) {
  25. if (isPrime(i)) {
  26. std::cout << i << ' ';
  27. }
  28. }
  29. std::cout << '\n';
  30. }
  31.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0 1 3 5 7 9 11 13 15 17 19 23 25 29 31 35 37 41 43 47 49 53 59 61 67 71 73 79 83 89 97