fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. bool eratosthenesSieve(int currentNumber) {
  8. /*
  9. Describe the function that checks if the number is prime and returns a boolean value.
  10. This function is based on the "sieve of Eratosthenes" algorithm.
  11. True - if the number is prime, false - normal.
  12. */
  13.  
  14. // If the number equals to 1 or if it even, return false. And if it equals to 2, return true.
  15. if (currentNumber == 2 or currentNumber == 3) {
  16. return true;
  17. } else if (!(currentNumber % 2) or !(currentNumber % 3) or currentNumber == 1) {
  18. return false;
  19. }
  20.  
  21. // If the number has the divider from 5 to the root of this number with the second step return false.
  22. for (int divider = 5; divider <= sqrt(currentNumber); divider += 2) {
  23. if (!(currentNumber % divider)) {
  24. return false;
  25. }
  26. }
  27.  
  28. // In all other situations, return true.
  29. return true;
  30. }
  31.  
  32.  
  33. void printSequenceOfPrimeNumbers(int beginning, int ending) {
  34. /*
  35. This function prints all prime numbers in a line.
  36. It contains a loop that starts and ends with the given values.
  37. */
  38.  
  39. for (int sequenceElement = beginning; sequenceElement <= ending; sequenceElement += 1) {
  40. if (eratosthenesSieve(sequenceElement)) {
  41. cout << sequenceElement << " ";
  42. }
  43. }
  44. }
  45.  
  46.  
  47. int main() {
  48. /*
  49. Receive the beginning and end of the sequence.
  50. */
  51.  
  52. int beginning, ending;
  53. cin >> beginning >> ending;
  54.  
  55. // Calling the function.
  56. printSequenceOfPrimeNumbers(beginning, ending);
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 4932KB
stdin
10 2
stdout
Standard output is empty