Given a number n, the task is to find the sum of all the factors.
Examples :
Input : n = 30
Output : 72
Dividers sum 1 + 2 + 3 + 5 + 6 +
10 + 15 + 30 = 72
Input : n = 15
Output : 24
Dividers sum 1 + 3 + 5 + 15 = 24
A simple solution is to traverse through all divisors and add them.
C++
#include<bits/stdc++.h>
using namespace std;
int divSum( int n)
{
if (n == 1)
return 1;
int result = 0;
for ( int i = 2; i <= sqrt (n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n/i);
}
}
return (result + n + 1);
}
int main()
{
int n = 30;
cout << divSum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int divSum( int n)
{
if (n == 1 )
return 1 ;
int result = 0 ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1 );
}
public static void main(String[] args)
{
int n = 30 ;
System.out.println(divSum(n));
}
}
|
Python3
import math
def divSum(n) :
if (n = = 1 ):
return 1
result = 0
for i in range ( 2 ,( int )(math.sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
if (i = = (n / i)) :
result = result + i
else :
result = result + (i + n / / i)
return (result + n + 1 )
n = 30
print (divSum(n))
|
C#
using System;
class GFG {
static int divSum( int n)
{
if (n == 1)
return 1;
int result = 0;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1);
}
public static void Main()
{
int n = 30;
Console.WriteLine(divSum(n));
}
}
|
PHP
<?php
function divSum( $n )
{
if ( $n == 1)
return 1;
$result = 0;
for ( $i = 2; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if ( $i == ( $n / $i ))
$result += $i ;
else
$result += ( $i + $n / $i );
}
}
return ( $result + $n + 1);
}
$n = 30;
echo divSum( $n );
?>
|
Javascript
<script>
function divSum(n)
{
if (n == 1)
return 1;
let result = 0;
for ( let i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (i == (n / i))
result += i;
else
result += (i + n / i);
}
}
return (result + n + 1);
}
let n = 30;
document.write(divSum(n));
</script>
|
Output :
72
Time Complexity: O(?n)
Auxiliary Space: O(1)
An efficient solution is to use below formula.
Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* … (pkak).
Sum of divisors = (1 + p1 + p12 ... p1a1) *
(1 + p2 + p22 ... p2a2) *
.............................................
(1 + pk + pk2 ... pkak)
We can notice that individual terms of above
formula are Geometric Progressions (GP). We
can rewrite the formula as.
Sum of divisors = (p1a1+1 - 1)/(p1 -1) *
(p2a2+1 - 1)/(p2 -1) *
..................................
(pkak+1 - 1)/(pk -1)
How does above formula work?
Consider the number 18.
Sum of factors = 1 + 2 + 3 + 6 + 9 + 18
Writing divisors as powers of prime factors.
Sum of factors = (20)(30) + (21)(30) + (2^0)(31) +
(21)(31) + (20)(3^2) + (2^1)(32)
= (20)(30) + (2^0)(31) + (2^0)(32) +
(21)(3^0) + (21)(31) + (21)(32)
= (20)(30 + 31 + 32) +
(21)(30 + 31 + 32)
= (20 + 21)(30 + 31 + 32)
If we take a closer look, we can notice that the
above expression is in the form.
(1 + p1) * (1 + p2 + p22)
Where p1 = 2 and p2 = 3 and 18 = 2132
So the task reduces to finding all prime factors and their powers.
C++
#include <bits/stdc++.h>
using namespace std;
int sumofFactors( int n)
{
int res = 1;
for ( int i = 2; i <= sqrt (n); i++)
{
int curr_sum = 1;
int curr_term = 1;
while (n % i == 0) {
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n >= 2)
res *= (1 + n);
return res;
}
int main()
{
int n = 30;
cout << sumofFactors(n);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
public class GFG{
static int sumofFactors( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= Math.sqrt(n); i++)
{
int curr_sum = 1 ;
int curr_term = 1 ;
while (n % i == 0 )
{
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n > 2 )
res *= ( 1 + n);
return res;
}
public static void main(String args[])
{
int n = 30 ;
System.out.println(sumofFactors(n));
}
}
|
Python3
import math as m
def sumofFactors(n):
res = 1
for i in range ( 2 , int (m.sqrt(n) + 1 )):
curr_sum = 1
curr_term = 1
while n % i = = 0 :
n = n / i;
curr_term = curr_term * i;
curr_sum + = curr_term;
res = res * curr_sum
if n > 2 :
res = res * ( 1 + n)
return res;
sum = sumofFactors( 30 )
print ( "Sum of all divisors is: " , sum )
|
C#
using System;
public class GFG {
static int sumofFactors( int n)
{
int res = 1;
for ( int i = 2; i <= Math.Sqrt(n); i++)
{
int curr_sum = 1;
int curr_term = 1;
while (n % i == 0)
{
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n > 2)
res *= (1 + n);
return res;
}
public static void Main()
{
int n = 30;
Console.WriteLine(sumofFactors(n));
}
}
|
PHP
<?php
function sumofFactors( $n )
{
$res = 1;
for ( $i = 2; $i <= sqrt( $n ); $i ++)
{
$curr_sum = 1;
$curr_term = 1;
while ( $n % $i == 0)
{
$n = $n / $i ;
$curr_term *= $i ;
$curr_sum += $curr_term ;
}
$res *= $curr_sum ;
}
if ( $n > 2)
$res *= (1 + $n );
return $res ;
}
$n = 30;
echo sumofFactors( $n );
?>
|
Javascript
<script>
function sumofFactors(n)
{
let res = 1;
for (let i = 2; i <= Math.sqrt(n); i++)
{
let curr_sum = 1;
let curr_term = 1;
while (n % i == 0)
{
n = n / i;
curr_term *= i;
curr_sum += curr_term;
}
res *= curr_sum;
}
if (n > 2)
res *= (1 + n);
return res;
}
let n = 30;
document.write(sumofFactors(n));
</script>
|
Output :
72
Time Complexity: O(?n log n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Further Optimization.
If there are multiple queries, we can use Sieve to find prime factors and their powers.
References:
https://www.math.upenn.edu/~deturck/m170/wk3/lecture/sumdiv.html
http://mathforum.org/library/drmath/view/71550.html
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Like Article
Save Article