Advertisement
Guest User

PrimeFactorization - MEHEDI

a guest
Oct 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<vector>
  4. using namespace std;
  5. bool seive[9999999];
  6. void primeFactor(int n);
  7. void prime(int n);
  8. int main()
  9. {
  10.     int n;
  11.  
  12.     while(1)
  13.     {
  14.         scanf("%d", &n);
  15.  
  16.     prime(n);
  17.     primeFactor(n);
  18.     }
  19.  
  20.  
  21.     //for(int i=2;i<n;i++)
  22.       //  if(!seive[i]) printf("%d\n", i);
  23.  
  24.  
  25.     return 0;
  26. }
  27. void primeFactor(int n)
  28. {
  29.     int x;
  30.     vector<int>pFact;
  31.     for(int i=2;i<=(int)sqrt(n);i++)
  32.     {
  33.         if(!seive[i])
  34.         {
  35.             if(n%i==0)
  36.             {
  37.  
  38.                 pFact.push_back(i);
  39.                 n/=i;
  40.                 while(!(n%i))
  41.                 {
  42.                     pFact.push_back(i);
  43.                     n/=i;
  44.  
  45.                 }
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51.     if(n>1) pFact.push_back(n);
  52.  
  53.     for(auto&i:pFact)cout<<i<<endl;
  54.  
  55.  
  56.  
  57.  
  58.  
  59. }
  60. void prime(int n)
  61. {
  62.     int j;
  63.     seive[0]=seive[1]=1;
  64.     for(int i=4;i<n;i+=2) seive[i]=1;
  65.     for(int i=3;i<=(int)sqrt(n);i+=2)
  66.     {
  67.         if(!seive[i])
  68.             for(j=i*i;j<n;j+=i) seive[j]=1;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement