Advertisement
niamul64

//prime factorisation

Sep 14th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1.  
  2. /////prime factorisation
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define sc(n) scanf("%lld",&n)
  7. int mark[3200000];
  8.  
  9. vector<long long> pr;//////vector of prime numbers
  10.  
  11. bool check(int N,int pos){
  12. return (N & (1<<pos));
  13. }
  14.  
  15. int Set(int N,int pos)
  16. {
  17.     return N=(N |(1<<pos));
  18. }
  19.  
  20. void bitwise(long long n){
  21. long long  i,j;
  22.     long long lim=sqrt(n);
  23.     pr.push_back(2);
  24.  
  25.     for(i=3;i<=n;i+=2){
  26.         if(check(mark[i/32],i%32)==0)
  27.         {
  28.  
  29.             pr.push_back(i);
  30.             if(i<=lim)
  31.             {
  32.                 for(j=i*i;j<=n;j+=2*i){
  33.                    mark[j/32]= Set(mark[j/32],j%32);
  34.  
  35.         }
  36.    }
  37.   }
  38.  }
  39. }
  40.  
  41. int main ()
  42. {
  43.     long long i,j;
  44.     long long n;
  45.     sc(n);
  46.     bitwise((long long)sqrt(n));
  47.     long long countt;
  48.     vector<long long> prFact;
  49.     vector<long long > factCount;
  50.     puts("\nprime factorization :\n");
  51.  
  52.     for(i=0;i<pr.size() && pr[i]<=sqrt(n);i++) {
  53.        countt=0;
  54.        if(!(n%pr[i]))  {
  55.            countt++;
  56.            n/=pr[i];
  57.            while(!(n%pr[i]))         {
  58.                n/=pr[i];
  59.                countt++;
  60.            }
  61.            prFact.push_back(pr[i]);
  62.            factCount.push_back(countt);
  63.            printf("%lld ^ %lld\n",pr[i],countt);
  64.        }
  65.    }
  66.  
  67.  
  68.    if(n>1){
  69.     printf("%lld ^ 1\n",n);
  70.     prFact.push_back(n);
  71.     factCount.push_back(1);
  72.    }
  73.  
  74.  
  75. long long div=1;
  76. long long odd=1;
  77. long long even=1;
  78. unsigned long long sumOF=1;
  79.  
  80.  
  81. for(i=0;i<factCount.size();i++)
  82. {
  83.     sumOF*=((pow((unsigned long long)prFact[i],(unsigned long long)factCount[i]+1)-1)/(prFact[i]-1));
  84.     div*=(factCount[i]+1);
  85.     if(prFact[i]%2)
  86.     odd*=(factCount[i]+1);
  87. }
  88.  
  89.  
  90.  
  91. printf("\n\n total prime factors are :  %d\n\n",prFact.size());
  92. printf("\n\ntotal number of divisors :  %lld\n\n",div);
  93. printf("\n\ntotal number of odd divisors :  %lld\n\n",odd);
  94. printf("\n\ntotal number of even divisors :  %lld\n\n",div-odd);
  95. printf("\n\nsum of :  %llu\n\n",sumOF);                                  
  96. return 0;      
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement