fork download
  1. #include<iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void SieveOfEratosthenes(long int n,long int z)
  6. {
  7. // Create a boolean array "prime[0..n]" and initialize
  8. // all entries it as true. A value in prime[i] will
  9. // finally be false if i is Not a prime, else true.
  10. bool prime[n+1];
  11. memset(prime, true, sizeof(prime));
  12.  
  13. for (long int p=2; p*p<=n; p++)
  14. {
  15. // If prime[p] is not changed, then it is a prime
  16. if (prime[p] == true)
  17. {
  18. // Update all multiples of p
  19. for (long int i=p*2; i<=n; i += p)
  20. prime[i] = false;
  21. }
  22. }
  23. if(z==1)
  24. z=2;
  25. // Print all prime numbers
  26. for (long int p=z; p<=n; p++)
  27. if (prime[p])
  28. cout << p << " ";
  29. }
  30.  
  31. // Driver Program to test above function
  32. int main()
  33. {
  34. long int a,b,n,i,k;
  35. cin>>n;
  36.  
  37. for(k=0;k<n;k++)
  38. {
  39. cin>>a>>b;
  40. SieveOfEratosthenes(b,a);
  41. cout<<endl;
  42. }
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3472KB
stdin
2
1 10
3 5
stdout
2 3 5 7 
3 5