Advertisement
Guest User

NIGERIAN

a guest
Mar 29th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. ll SieveOfEratosthenes(ll n)
  6. { ll cnt;
  7. bool prime[n+1];
  8. memset(prime, true, sizeof(prime));
  9.  
  10. for (ll p=2; p*p<=n; p++)
  11. {
  12. if (prime[p] == true)
  13. {
  14. for (ll i=p*p; i<=n; i += p)
  15. prime[i] = false;
  16. }
  17. }
  18. for (ll p=2; p<=n; p++)
  19. if (prime[p]) cnt++;
  20.  
  21. return cnt;
  22. }
  23.  
  24.  
  25. int main()
  26. { ios_base::sync_with_stdio(false);
  27. cin.tie(NULL);
  28.  
  29. ll q; cin >> q;
  30.  
  31. while(q--){
  32. ll l,r; cin >> l >> r;
  33. if(r>l){
  34. cout << SieveOfEratosthenes(r)-SieveOfEratosthenes(l) << "\n";
  35. }
  36. if(l>=r){
  37. cout << SieveOfEratosthenes(l)-SieveOfEratosthenes(r) << "\n";
  38. }
  39. }
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement