Sum of Factors of a Number (original) (raw)

Last Updated : 5 Apr, 2026

Given a number n, find the sum of all the factors.

**Examples :

**Input: n = 30
**Output: 72
**Explanation: Divisors sum 1 + 2 + 3 + 5 + 6 + 10 + 15 + 30 = 72

**Input: n = 15
**Output: 24
**Explanation: Divisors sum 1 + 3 + 5 + 15 = 24

Try It Yourselfredirect icon

Table of Content

Iterative Method - O(√n) Time and O(1) Space

Traverse from 2 to √n because factors always occur in pairs (i, n / i), so we can find all divisors till √n. Initialize the res with 1 (already including factor 1). For every divisor found, add both i and (n / i), handling the perfect square case where both are equal. Finally, add n to get the total sum.

For n = 15:

Final Answer = 24

C++ `

#include #include using namespace std;

int factorSum(int n) { if (n == 1) return 1;

// include 1
int res = 1;

for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
        if (i == n / i)
            res += i;
        else
            res += (i + n / i);
    }
}

// include n
return res + n;

}

int main() { int n = 30; cout << factorSum(n); return 0; }

Java

import java.io.*;

class GFG { static int factorSum(int n) {

    if (n == 1)
        return 1;

    // include 1
    int res = 1;

    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
            
            // check perfect square case
            if (i == n / i)
                res += i;
            else
                res += (i + n / i);
        }
    }
    
    // include n
    return res + n;
}

public static void main(String[] args) {
    int n = 30;
    System.out.println(factorSum(n));
}

}

Python

import math

def factorSum(n):

if n == 1:
    return 1

# include 1
res = 1

for i in range(2, int(math.sqrt(n)) + 1):
    if n % i == 0:
        
        # check perfect square case
        if i == n // i:
            res += i
        else:
            res += (i + n // i)

# include n
return res + n

if name == "main": n = 30 print(factorSum(n))

C#

using System;

class GFG {

static int factorSum(int n)
{
    if (n == 1)
        return 1;
    
    // include 1
    int res = 1;

    for (int i = 2; i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
        {
            // check perfect square case
            if (i == n / i)
                res += i;
            else
                res += (i + n / i);
        }
    }
    
    // include n
    return res + n;
}

public static void Main()
{
    int n = 30;
    Console.WriteLine(factorSum(n));
}

}

JavaScript

function factorSum(n) { if (n == 1) return 1;

// include 1
let res = 1;

for (let i = 2; i <= Math.sqrt(n); i++)
{
    if (n % i == 0)
    {
        // check perfect square case
        if (i == Math.floor(n / i))
            res += i;
        else
            res += (i + Math.floor(n / i));
    }
}

// include n
return res + n;

}

// Driver Code let n = 30; console.log(factorSum(n));

`

Keep Dividing n with Prime Factors

Use the prime factorization of n and apply the geometric progression formula to compute the sum of divisors efficiently. Each prime factor contributes a series (1 + p + p2 + ... + pa), and multiplying all such series gives the final sum.

If, n = p1a1 x p2a2 x ... x pkak

Then, Sum of factors = (1 + p1 + p12 + ... + p1a1) x (1 + p2 + p22 + ... + p2a2) x ... x (1 + pk + pk2 + ... + pkak)

We can notice that individual terms of the above formula are Geometric Progressions (GP). So, we can rewrite it as:

\text{Sum of factors} =\frac{p_1^{a_1+1} - 1}{p_1 - 1} \times\frac{p_2^{a_2+1} - 1}{p_2 - 1} \times \cdots \times\frac{p_k^{a_k+1} - 1}{p_k - 1}

Consider the number n = 18

18 = 2¹ × 3²
Factors = 1, 2, 3, 6, 9, 18

Consider each prime Factor and add all its powers divisible by n
Sum = (2⁰ + 2¹) × (3⁰ + 3¹ + 3²)
= (1 + 2) × (1 + 3 + 9)
= (1 + p₁) × (1 + p₂ + p₂²)

So the problem reduces to finding the prime factors of n along with their powers.

C++ `

#include #include using namespace std;

long factorSum(int n) { // final result int res = 1;

for (int i = 2; i <= sqrt(n); i++)
{
    // current factor contribution
    int curr_sum = 1;
    int curr_term = 1;

    while (n % i == 0)
    {
        // Divide n by i to remove this prime factor completely.
        // This reduces n and avoids 
        // redundant checks in future iterations.
        n = n / i;
        
        curr_term *= i;
        curr_sum += curr_term;
    }

    res *= curr_sum;
}

// if n is prime
if (n >= 2)
    res *= (1 + n);

return res;

}

int main() { int n = 30; cout << factorSum(n); return 0; }

Java

import java.lang.Math;

class GFG {

static int factorSum(int n)
{
    // final result
    int res = 1;

    for (int i = 2; i <= Math.sqrt(n); i++)
    {
        // current factor contribution
        int curr_sum = 1;
        int curr_term = 1;

        while (n % i == 0)
        {
            // Divide n by i to remove this prime factor completely.
            // This reduces n and avoids 
            // redundant checks in future iterations.
            n = n / i;
            
            curr_term *= i;
            curr_sum += curr_term;
        }

        res *= curr_sum;
    }
    
    // if n is prime
    if (n >= 2)
        res *= (1 + n);

    return res;
}

public static void main(String[] args)
{
    int n = 30;
    System.out.println(factorSum(n));
}

}

Python

import math

def factorSum(n):

# final result
res = 1

for i in range(2, int(math.sqrt(n)) + 1):
    
    # current factor contribution
    curr_sum = 1
    curr_term = 1

    while n % i == 0:
        
        # Divide n by i to remove this prime factor completely.
        # This reduces n and avoids 
        # redundant checks in future iterations.
        n = n // i
        
        curr_term *= i
        curr_sum += curr_term

    res *= curr_sum

# if n is prime
if n >= 2:
    res *= (1 + n)

return res

if name == "main": n = 30 print(factorSum(n))

C#

using System;

class GFG {

static long factorSum(int n)
{
    // final result
    int res = 1;

    for (int i = 2; i <= Math.Sqrt(n); i++)
    {
        // current factor contribution
        int curr_sum = 1;
        int curr_term = 1;

        while (n % i == 0)
        {
            // Divide n by i to remove this prime factor completely.
            // This reduces n and avoids 
            // redundant checks in future iterations.
            n = n / i;
            
            curr_term *= i;
            curr_sum += curr_term;
        }

        res *= curr_sum;
    }
    
    // if n is prime
    if (n >= 2)
        res *= (1 + n);

    return res;
}

public static void Main()
{
    int n = 30;
    Console.WriteLine(factorSum(n));
}

}

JavaScript

function factorSum(n) { // final result let res = 1;

for (let i = 2; i <= Math.sqrt(n); i++)
{
    // current factor contribution
    let curr_sum = 1;
    let curr_term = 1;

    while (n % i == 0)
    {
        // Divide n by i to remove this prime factor completely.
        // This reduces n and avoids 
        // redundant checks in future iterations.
        n = Math.floor(n / i);
        
        curr_term *= i;
        curr_sum += curr_term;
    }

    res *= curr_sum;
}

// if n is prime
if (n >= 2)
    res *= (1 + n);

return res;

}

// Driver Code let n = 30; console.log(factorSum(n));

`