Advertisement
a53

Cate numere prime sunt pana la n

a53
Mar 23rd, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <iostream>
  2. #define N 100000000/2/8+1
  3. using namespace std;
  4. char p[N];
  5. /// https://infogenius.ro/ciurul-lui-eratostene-cpp/
  6. int Ciur(int n)
  7. {
  8. int i,j,nr=1;
  9. for(i=1;((i*i)<<1)+(i<<1)<=n;i+=1)
  10. {
  11. if((p[i>>3]&(1<<(i&7)))==0)
  12. {
  13. for(j=((i*i)<<1)+(i<<1);(j<<1)+1<=n;j+=(i<<1)+1)
  14. {
  15. p[j>>3]|=(1<<(j&7));
  16. }
  17. }
  18. }
  19. for(i=1;2*i+1<=n;++i)
  20. if((p[i>>3]&(1<<(i&7)))==0)
  21. ++nr;
  22. return nr;
  23. }
  24.  
  25. int main()
  26. {
  27. int n;
  28. cin>>n;
  29. cout<<Ciur(n);
  30. return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement