Posts

Print sum of prime divisor

#include <bits/stdc++.h> using namespace std; bool isprime (int n) {     if(n<=1)return false;     if(n<=3)return true;     if(n%2==0 || n%3==0)         return false;     for(int i=5; i*i<=n; i+=6)     {         if(n%i==0 || n%(i+2)==0)             return false;     }     return true; } int sumofdivisor(int n) {     int sum=0;     for(int i=2; i<=n; i++)     {         if(n%i==0)         {             if(isprime(i))             {                 sum+=i;             }         }     }     return sum; } int main () {     int t;     cin >> t;     while(t--)     {         int x;         cin >> x;         cout << sumofdivisor(x) << endl;     }     return 0; }

Print Efficiency Prime Divisor

#include <bits/stdc++.h> using namespace std; void primefactor(int n) { while(n%2==0) { cout << 2 << " "; n=n/2; } for(int i=3; i<=sqrt(n); i+=2) { while(n%i==0) { cout << i << " "; n=n/i; } } if(n>2){ cout << n << endl; } } int main () { int n; cin >> n; primefactor(n); return 0; }

Finding Summations of Divisor

Number of primefactorize(n) = p1^x1*p2^x2*p3^x3....pn^xn summation of divisor(n)=1+r+r^2+r^3......+r^n=(r^n+1-1)/r-1 #include <bits/stdc++.h> using namespace std; int divsum(int n) {   bool mark[n+1];   memset(mark, true, sizeof(mark));   for(int i=2; i*i<=n; i++)   {   if(mark[i]==true)   {   for(int j=i*2; j<=n; j+=i)   {   mark[j]=false;   }   }   }   int total=1, ret=0;   for(int i=2; i<=(n); i++)   {   if(mark[i]==true)   {   int cnt=0;   if(n%i==0)   {   while(n%i==0)   {   n/=i;   cnt++;   } ret = (pow(i, cnt+1)-1)/(i-1);   }   total = total*ret;   }   }   return total; } int main () { int n; cin >> n; cout << divsum(n)<<endl; return 0; }

Efficiently getting all divisors of a given number

Here's my code: #include <bits/stdc++.h> using namespace std; #define pii pair<int, int> #define MAX 46656 #define LMT 216 #define LEN 4830 #define RNG 100032 unsigned base[MAX / 64], segment[RNG / 64], primes[LEN]; #define sq(x) ((x)*(x)) #define mset(x,v) memset(x,v,sizeof(x)) #define chkC(x,n) (x[n>>6]&(1<<((n>>1)&31))) #define setC(x,n) (x[n>>6]|=(1<<((n>>1)&31))) // http://zobayer.blogspot.com/2009/09/segmented-sieve.html void sieve() {     unsigned i, j, k;     for (i = 3; i<LMT; i += 2)         if (!chkC(base, i))             for (j = i*i, k = i << 1; j<MAX; j += k)                 setC(base, j);     primes[0] = 2;     for (i = 3, j = 1; i<MAX; i += 2)         if (!chkC(base, i))             primes[j++] = i; } //http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ vector <pii> factors; void primeFactors(int num) {  

Total number of divisors for a given number using prime Factorization

#include <bits/stdc++.h> using namespace std; int divCount(int n) { bool mark[n+1]; memset(mark, true, sizeof(mark)); for(int i=2; i*i<=n; i++) { if(mark[i]==true) { for(int j=i*2; j<n; j+=i) { mark[j]=false; } } } int total=1; for(int i=2; i<=n; i++) { if(mark[i]) { int cnt=0; if(n%i==0) { while(n%i==0) { n/=i; cnt++; } total=total*(cnt+1); } } } return total; } int main () { int n; cin >> n; cout << divCount(n) << endl; return 0; } another example: //this is my work__ finding number of divisor #include <bits/stdc++.h> using namespace std; bool isprime(int x) { for(int i=2; i*i<=x; i++) { if(x%i==0){ return false; } } return false; } int divcount(int n) { int total=1; for(int i=2; i<=n; i++) { if(isprime(i)==false) { int cnt=0

Decimal to Binary

#include <bits/stdc++.h> using namespace std ; void dectobinary ( int n ) { int binarynum [ 32 ]; int i = 0 ; while ( n != 0 ) { binarynum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) { cout << binarynum [ j ]; } } int main () { int n ; cin >> n ; dectobinary ( n ); return 0 ; }

Binary to Decimal

#include <bits/stdc++.h> using namespace std ; int binarytodec ( long long n ) { int dec_number = 0 ; int i = 0 , rem ; while ( n != 0 ) { rem = n % 10 ; n = n / 10 ; dec_number = dec_number + rem * pow ( 2 , i ); i ++ ; } return dec_number ; } int main () { long long n ; cin >> n ; cout << binarytodec ( n ) << endl ; return 0 ; } Another Example: #include <bits/stdc++.h> using namespace std ; int main () { char str [ 100 ]; cin . getline ( str , 100 ); int value = 0 ; int len = strlen ( str ) - 1 ; for ( int i = 0 ; str [ i ] != '\0' ; i ++ ) { value = value + ( str [ i ] - '0' ) * pow ( 2 , len ); len -- ; } cout <<