Spoj TDPRIMES - Printing some primes Solution




Problem Link:  https://www.spoj.com/problems/TDPRIMES/

 Solution in C++:  

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<bool>v(100000000,true);
  4. int ar[8000000];
  5. int main()
  6. {
  7. long long int n=100000000,i,j;
  8. v[1]=v[0]=false;
  9. for(i=4; i<=n; i+=2)v[i]=false;
  10. for(i=3; i*i<=n; i+=2)
  11. {
  12. if(v[i])
  13. {
  14. for(j=i*i; j<=n; j=j+(2*i))
  15. {
  16. v[j]=false;
  17. }
  18. }
  19. }
  20. j=0;
  21. ar[0]=2;
  22. for(i=3; i<=n; i+=2)
  23. {
  24. if(v[i])
  25. ar[++j]=i;
  26. }
  27. for(i=1; i<=j; i+=100)
  28. cout<<ar[i-1]<<endl;
  29. return 0;
  30. }
  31.  

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.