VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-of-factors-of-a-number-using-prime-factorization/

⇱ Sum of Factors of a Number using Prime Factorization - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of Factors of a Number using Prime Factorization

Last Updated : 20 Mar, 2023

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, 

A dry run is as shown below as follows: 

(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 
= 2604
So, the sum of all factors of 1100 = 2604 


Below is the implementation of the above approach: 


Output: 
Sum of the factors is : 28

 

Time Complexity: O(N)

Auxiliary Space: O(n) // an extra n sized array has been created.

Comment