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 925 (Div. 3) 1931D. Divisible Pairs Solution

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

Powered by Blogger.