fork download
  1. //make a program to take an array and tell all sum of number of prime factors of it..
  2. //so make a seive array of the least prime factor of all numbers
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define n 1000000
  6. int main(){
  7.  
  8. vector<int> leasp(n,0);
  9. leasp[0]=0;
  10. leasp[1]=0;
  11. for(int i=2;i<=n;i+=2)
  12. {
  13. leasp[i]=2;
  14. }
  15. for(int i=3;i*i<=n;i+=2)
  16. {
  17. if(leasp[i]==0){//the inner loop would run only for primes
  18. for(int j=i*i;j<n;j+=2*i)
  19. {
  20. if(leasp[j]==0){
  21. leasp[j]=i;
  22. }
  23. }
  24. }
  25. }
  26. //then for all of the primes we have to put that value to it
  27. for(int i=2;i<n;i++)
  28. {
  29. if(leasp[i]==0)
  30. {
  31. leasp[i]=i;
  32. }
  33. }
  34. for(int i=0;i<50;i++)
  35. {
  36.  
  37. cout<<leasp[i]<< " ";
  38. }
  39. }
  40.  
Success #stdin #stdout 0.01s 6952KB
stdin
Standard input is empty
stdout
0 0 2 3 2 5 2 7 2 3 2 11 2 13 2 3 2 17 2 19 2 3 2 23 2 5 2 3 2 29 2 31 2 3 2 5 2 37 2 3 2 41 2 43 2 3 2 47 2 7