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 = 72Input: n = 15
Output: 24
Explanation: Divisors sum 1 + 3 + 5 + 15 = 24
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:
- Initialize res = 1 (including factor 1)
- √15 ≈ 3, so loop runs from i = 2 to 3
- i = 2, 15 is not divisible by 2, so skip
- i = 3, 15 is divisible by 3, so factors are 3 and 5, res becomes 1 + 3 + 5 = 9
- Add n itself, res becomes 9 + 15 = 24
Final Answer = 24
#include <iostream>
#include <cmath>
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;
}
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));
}
}
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))
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));
}
}
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));
Output
72
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:
Consider the number n = 18
18 = 2¹ × 3²
Factors = 1, 2, 3, 6, 9, 18Consider 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.
#include <iostream>
#include <cmath>
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;
}
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));
}
}
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))
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));
}
}
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));
Output
72