Sum of Factors of a Number using Prime Factorization - GeeksforGeeks

19 May, 2021
Given a number N. The task is to find the sum of all factors of the given number N.
Examples:  
Input : N = 12
Output : 28
All factors of 12 are: 1,2,3,4,6,12

Input : 60
Output : 168 
Approach: Suppose N = 1100, the idea is to first find the prime factorization of the given number N. Therefore, the prime factorization of 1100 = 22 * 52 * 11.So, the formula to calculate the sum of all factors can be given as, 
(20 + 21 + 22) * (50 + 51 + 52) * (110 + 111) (upto the power of factor in factorization i.e. power of 2 and 5 is 2 and 11 is 1.) = (1 + 2 + 22) * (1 + 5 + 52) * (1 + 11) = 7 * 31 * 12 = 2604So, the sum of all factors of 1100 = 2604 
Below is the implementation of the above approach: 
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find sum of all// factors of a given number #include <bits/stdc++.h> using namespace std; // Using SieveOfEratosthenes to find smallest prime// factor of all the numbers.// For example, if N is 10,// s[2] = s[4] = s[6] = s[10] = 2// s[3] = s[9] = 3// s[5] = 5// s[7] = 7void sieveOfEratosthenes(int N, int s[]){    // Create a boolean array "prime[0..n]" and    // initialize all entries in it as false.    vector<bool> prime(N + 1, false);     // Initializing smallest factor equal to 2    // for all the even numbers    for (int i = 2; i <= N; i += 2)        s[i] = 2;     // For odd numbers less then equal to n    for (int i = 3; i <= N; i += 2) {        if (prime[i] == false) {             // s(i) for a prime is the number itself            s[i] = i;             // For all multiples of current prime number            for (int j = i; j * i <= N; j += 2) {                if (prime[i * j] == false) {                    prime[i * j] = true;                     // i is the smallest prime factor for                    // number "i*j".                    s[i * j] = i;                }            }        }    }} // Function to find sum of all prime factorsint findSum(int N){    // Declaring array to store smallest prime    // factor of i at i-th index    int s[N + 1];     int ans = 1;     // Filling values in s[] using sieve    sieveOfEratosthenes(N, s);         int currFactor = s[N]; // Current prime factor of N    int power = 1; // Power of current prime factor     while (N > 1) {        N /= s[N];         // N is now N/s[N]. If new N als has smallest        // prime factor as currFactor, increment power        if (currFactor == s[N]) {            power++;            continue;        }         int sum = 0;                 for(int i=0; i<=power; i++)            sum += pow(currFactor,i);                 ans *= sum;                          // Update current prime factor as s[N] and        // initializing power of factor as 1.        currFactor = s[N];        power = 1;    }     return ans;} // Driver codeint main(){    int n = 12;     cout << "Sum of the factors is : ";     cout << findSum(n);         return 0;}
//Java Program to find sum of all//factors of a given numberpublic class GFG {     //Using SieveOfEratosthenes to find smallest prime    //factor of all the numbers.    //For example, if N is 10,    //s[2] = s[4] = s[6] = s[10] = 2    //s[3] = s[9] = 3    //s[5] = 5    //s[7] = 7    static void sieveOfEratosthenes(int N, int s[])    {     // Create a boolean array "prime[0..n]" and     // initialize all entries in it as false.     boolean[] prime = new boolean[N + 1];           for(int i = 0; i < N+1; i++)         prime[i] = false;      // Initializing smallest factor equal to 2     // for all the even numbers     for (int i = 2; i <= N; i += 2)         s[i] = 2;      // For odd numbers less then equal to n     for (int i = 3; i <= N; i += 2) {         if (prime[i] == false) {              // s(i) for a prime is the number itself             s[i] = i;              // For all multiples of current prime number             for (int j = i; j * i <= N; j += 2) {                 if (prime[i * j] == false) {                     prime[i * j] = true;                      // i is the smallest prime factor for                     // number "i*j".                     s[i * j] = i;                 }             }         }     }    }     //Function to find sum of all prime factors    static int findSum(int N)    {     // Declaring array to store smallest prime     // factor of i at i-th index     int[] s = new int[N + 1];      int ans = 1;      // Filling values in s[] using sieve     sieveOfEratosthenes(N, s);            int currFactor = s[N]; // Current prime factor of N     int power = 1; // Power of current prime factor      while (N > 1) {         N /= s[N];          // N is now N/s[N]. If new N als has smallest         // prime factor as currFactor, increment power         if (currFactor == s[N]) {             power++;             continue;         }          int sum = 0;                    for(int i=0; i<=power; i++)             sum += Math.pow(currFactor,i);                    ans *= sum;                               // Update current prime factor as s[N] and         // initializing power of factor as 1.         currFactor = s[N];         power = 1;     }      return ans;    }     //Driver code    public static void main(String[] args) {                 int n = 12;          System.out.print("Sum of the factors is : ");          System.out.print(findSum(n));    }}
# Python 3 Program to find# sum of all factors of a# given number # Using SieveOfEratosthenes# to find smallest prime# factor of all the numbers.# For example, if N is 10,# s[2] = s[4] = s[6] = s[10] = 2# s[3] = s[9] = 3# s[5] = 5# s[7] = 7def sieveOfEratosthenes(N, s) :     # Create a boolean list "prime[0..n]"    # and initialize all entries in it    # as false.    prime = [False] * (N + 1)     # Initializing smallest    # factor equal to 2 for    # all the even numbers    for i in range(2, N + 1, 2) :        s[i] = 2     # For odd numbers less    # then equal to n    for i in range(3, N + 1, 2) :         if prime[i] == False :             # s[i] for a prime is            # the number itself            s[i] = i             # For all multiples of            # current prime number            for j in range(i, (N + 1) // i, 2) :                                 if prime[i * j] == False :                    prime[i * j] = True                                         # i is the smallest                    # prime factor for                    # number "i*j".                    s[i * j] = i                 #J += 2 # Function to find sum# of all prime factorsdef findSum(N) :     # Declaring list to store    # smallest prime factor of    # i at i-th index    s = [0] * (N + 1)    ans = 1     # Filling values in s[] using    # sieve function calling    sieveOfEratosthenes(N, s)     # Current prime factor of N    currFactor = s[N]     # Power of current prime factor    power = 1     while N > 1 :        N //= s[N]         # N is now N//s[N]. If new N        # also has smallest prime        # factor as currFactor,        # increment power        if currFactor == s[N] :            power += 1            continue         sum = 0         for i in range(power + 1) :            sum += pow(currFactor, i)         ans *= sum         # Update current prime factor        # as s[N] and initializing        # power of factor as 1.        currFactor = s[N]        power = 1             return ans # Driver Codeif __name__ == "__main__" :     n = 12    print("Sum of the factors is :", end = " ")    print(findSum(n))     # This code is contributed by ANKITRAI1
// C# Program to find sum of all// factors of a given numberusing System; class GFG{ // Using SieveOfEratosthenes to find smallest// prime factor of all the numbers.// For example, if N is 10,// s[2] = s[4] = s[6] = s[10] = 2// s[3] = s[9] = 3// s[5] = 5// s[7] = 7static void sieveOfEratosthenes(int N, int []s){     // Create a boolean array "prime[0..n]" and// initialize all entries in it as false.bool[] prime = new bool[N + 1]; for(int i = 0; i < N + 1; i++)    prime[i] = false; // Initializing smallest factor equal// to 2 for all the even numbersfor (int i = 2; i <= N; i += 2)    s[i] = 2; // For odd numbers less then equal to nfor (int i = 3; i <= N; i += 2){    if (prime[i] == false)    {         // s(i) for a prime is the        // number itself        s[i] = i;         // For all multiples of current        // prime number        for (int j = i; j * i <= N; j += 2)        {            if (prime[i * j] == false)            {                prime[i * j] = true;                 // i is the smallest prime factor                // for number "i*j".                s[i * j] = i;            }        }    }}} // Function to find sum of all// prime factorsstatic int findSum(int N){// Declaring array to store smallest// prime factor of i at i-th indexint[] s = new int[N + 1]; int ans = 1; // Filling values in s[] using sievesieveOfEratosthenes(N, s); int currFactor = s[N]; // Current prime factor of Nint power = 1; // Power of current prime factor while (N > 1){    N /= s[N];     // N is now N/s[N]. If new N als has smallest    // prime factor as currFactor, increment power    if (currFactor == s[N])    {        power++;        continue;    }     int sum = 0;         for(int i = 0; i <= power; i++)        sum += (int)Math.Pow(currFactor, i);         ans *= sum;         // Update current prime factor as s[N]    // and initializing power of factor as 1.    currFactor = s[N];    power = 1;} return ans;} // Driver codepublic static void Main(){    int n = 12;     Console.Write("Sum of the factors is : ");     Console.WriteLine(findSum(n));}} // This code is contributed by Shashank
<?php// PHP Program to find sum of all// factors of a given number // Using SieveOfEratosthenes to find smallest prime// factor of all the numbers.// For example, if N is 10,// s[2] = s[4] = s[6] = s[10] = 2// s[3] = s[9] = 3// s[5] = 5// s[7] = 7function sieveOfEratosthenes($N, &$s){    // Create a boolean array "prime[0..n]" and    // initialize all entries in it as false.    $prime=array_fill(0,$N + 1, false);     // Initializing smallest factor equal to 2    // for all the even numbers    for ($i = 2; $i <= $N; $i += 2)        $s[$i] = 2;     // For odd numbers less then equal to n    for ($i = 3; $i <= $N; $i += 2) {        if ($prime[$i] == false) {             // s(i) for a prime is the number itself            $s[$i] = $i;             // For all multiples of current prime number            for ($j = $i; $j * $i <= $N; $j += 2) {                if ($prime[$i * $j] == false) {                    $prime[$i * $j] = true;                     // i is the smallest prime factor for                    // number "i*j".                    $s[$i * $j] = $i;                }            }        }    }} // Function to find sum of all prime factorsfunction findSum($N){    // Declaring array to store smallest prime    // factor of i at i-th index    $s=array_fill(0,$N + 1,0);     $ans = 1;     // Filling values in s[] using sieve    sieveOfEratosthenes($N, $s);         $currFactor = $s[$N]; // Current prime factor of N    $power = 1; // Power of current prime factor     while ($N > 1) {        $N /= $s[$N];         // N is now N/s[N]. If new N als has smallest        // prime factor as currFactor, increment power        if ($currFactor == $s[$N]) {            $power++;            continue;        }        $sum = 0;                 for($i=0; $i<=$power; $i++)            $sum += (int)pow($currFactor,$i);                 $ans *= $sum;                          // Update current prime factor as s[N] and        // initializing power of factor as 1.        $currFactor = $s[$N];        $power = 1;    }     return $ans;} // Driver code     $n = 12;     echo "Sum of the factors is : ";     echo findSum($n);     // This code is contributed by mits?>
<script>//Javascript Program to find sum of all//factors of a given number         //Using SieveOfEratosthenes to find smallest prime    //factor of all the numbers.    //For example, if N is 10,    //s[2] = s[4] = s[6] = s[10] = 2    //s[3] = s[9] = 3    //s[5] = 5    //s[7] = 7    function sieveOfEratosthenes(N,s)    {        // Create a boolean array "prime[0..n]" and         // initialize all entries in it as false.         let prime = new Array(N + 1);                 for(let i = 0; i < N+1; i++)            prime[i] = false;            // Initializing smallest factor equal to 2         // for all the even numbers         for (let i = 2; i <= N; i += 2)            s[i] = 2;            // For odd numbers less then equal to n         for (let i = 3; i <= N; i += 2) {            if (prime[i] == false) {                   // s(i) for a prime is the number itself                s[i] = i;                    // For all multiples of current prime number                 for (let j = i; j * i <= N; j += 2) {                    if (prime[i * j] == false) {                        prime[i * j] = true;                            // i is the smallest prime factor for                         // number "i*j".                         s[i * j] = i;                     }                 }             }         }    }         //Function to find sum of all prime factors    function findSum(N)    {        // Declaring array to store smallest prime         // factor of i at i-th index         let s = new Array(N + 1);            let ans = 1;            // Filling values in s[] using sieve         sieveOfEratosthenes(N, s);                  let currFactor = s[N]; // Current prime factor of N         let power = 1; // Power of current prime factor            while (N > 1) {            N = Math.floor(N/s[N]);                // N is now N/s[N]. If new N als has smallest             // prime factor as currFactor, increment power             if (currFactor == s[N]) {                 power++;                 continue;             }                let sum = 0;                          for(let i=0; i<=power; i++)                sum += Math.pow(currFactor,i);                          ans *= sum;                                       // Update current prime factor as s[N] and             // initializing power of factor as 1.             currFactor = s[N];             power = 1;         }            return ans;    }         //Driver code    let n = 12;    document.write("Sum of the factors is : ");    document.write(findSum(n));           // This code is contributed by rag2127</script>
Sum of the factors is : 28
 
ankthon
ukasp
Shashank12
Mithun Kumar
rag2127
factor
prime-factor
sieve
Mathematical
Mathematical
sieve
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array
Minimum number of jumps to reach end