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:
C++
#include <bits/stdc++.h>
using namespace std;
void sieveOfEratosthenes(int N, int s[])
{
vector<bool> prime(N + 1, false);
for (int i = 2; i <= N; i += 2)
s[i] = 2;
for (int i = 3; i <= N; i += 2) {
if (prime[i] == false) {
s[i] = i;
for (int j = i; j * i <= N; j += 2) {
if (prime[i * j] == false) {
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
int findSum(int N)
{
int s[N + 1];
int ans = 1;
sieveOfEratosthenes(N, s);
int currFactor = s[N];
int power = 1;
while (N > 1) {
N /= s[N];
if (currFactor == s[N]) {
power++;
continue;
}
int sum = 0;
for(int i=0; i<=power; i++)
sum += pow(currFactor,i);
ans *= sum;
currFactor = s[N];
power = 1;
}
return ans;
}
int main()
{
int n = 12;
cout << "Sum of the factors is : ";
cout << findSum(n);
return 0;
}
|
Java
public class GFG {
static void sieveOfEratosthenes(int N, int s[])
{
boolean[] prime = new boolean[N + 1];
for(int i = 0; i < N+1; i++)
prime[i] = false;
for (int i = 2; i <= N; i += 2)
s[i] = 2;
for (int i = 3; i <= N; i += 2) {
if (prime[i] == false) {
s[i] = i;
for (int j = i; j * i <= N; j += 2) {
if (prime[i * j] == false) {
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
static int findSum(int N)
{
int[] s = new int[N + 1];
int ans = 1;
sieveOfEratosthenes(N, s);
int currFactor = s[N];
int power = 1;
while (N > 1) {
N /= s[N];
if (currFactor == s[N]) {
power++;
continue;
}
int sum = 0;
for(int i=0; i<=power; i++)
sum += Math.pow(currFactor,i);
ans *= sum;
currFactor = s[N];
power = 1;
}
return ans;
}
public static void main(String[] args) {
int n = 12;
System.out.print("Sum of the factors is : ");
System.out.print(findSum(n));
}
}
|
Python 3
def sieveOfEratosthenes(N, s) :
prime = [False] * (N + 1)
for i in range(2, N + 1, 2) :
s[i] = 2
for i in range(3, N + 1, 2) :
if prime[i] == False :
s[i] = i
for j in range(i, (N + 1) // i, 2) :
if prime[i * j] == False :
prime[i * j] = True
s[i * j] = i
def findSum(N) :
s = [0] * (N + 1)
ans = 1
sieveOfEratosthenes(N, s)
currFactor = s[N]
power = 1
while N > 1 :
N //= s[N]
if currFactor == s[N] :
power += 1
continue
sum = 0
for i in range(power + 1) :
sum += pow(currFactor, i)
ans *= sum
currFactor = s[N]
power = 1
return ans
if __name__ == "__main__" :
n = 12
print("Sum of the factors is :", end = " ")
print(findSum(n))
|
C#
using System;
class GFG
{
static void sieveOfEratosthenes(int N, int []s)
{
bool[] prime = new bool[N + 1];
for(int i = 0; i < N + 1; i++)
prime[i] = false;
for (int i = 2; i <= N; i += 2)
s[i] = 2;
for (int i = 3; i <= N; i += 2)
{
if (prime[i] == false)
{
s[i] = i;
for (int j = i; j * i <= N; j += 2)
{
if (prime[i * j] == false)
{
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
static int findSum(int N)
{
int[] s = new int[N + 1];
int ans = 1;
sieveOfEratosthenes(N, s);
int currFactor = s[N];
int power = 1;
while (N > 1)
{
N /= s[N];
if (currFactor == s[N])
{
power++;
continue;
}
int sum = 0;
for(int i = 0; i <= power; i++)
sum += (int)Math.Pow(currFactor, i);
ans *= sum;
currFactor = s[N];
power = 1;
}
return ans;
}
public static void Main()
{
int n = 12;
Console.Write("Sum of the factors is : ");
Console.WriteLine(findSum(n));
}
}
|
PHP
<?php
function sieveOfEratosthenes($N, &$s)
{
$prime=array_fill(0,$N + 1, false);
for ($i = 2; $i <= $N; $i += 2)
$s[$i] = 2;
for ($i = 3; $i <= $N; $i += 2) {
if ($prime[$i] == false) {
$s[$i] = $i;
for ($j = $i; $j * $i <= $N; $j += 2) {
if ($prime[$i * $j] == false) {
$prime[$i * $j] = true;
$s[$i * $j] = $i;
}
}
}
}
}
function findSum($N)
{
$s=array_fill(0,$N + 1,0);
$ans = 1;
sieveOfEratosthenes($N, $s);
$currFactor = $s[$N];
$power = 1;
while ($N > 1) {
$N /= $s[$N];
if ($currFactor == $s[$N]) {
$power++;
continue;
}
$sum = 0;
for($i=0; $i<=$power; $i++)
$sum += (int)pow($currFactor,$i);
$ans *= $sum;
$currFactor = $s[$N];
$power = 1;
}
return $ans;
}
$n = 12;
echo "Sum of the factors is : ";
echo findSum($n);
?>
|
Javascript
<script>
function sieveOfEratosthenes(N,s)
{
let prime = new Array(N + 1);
for(let i = 0; i < N+1; i++)
prime[i] = false;
for (let i = 2; i <= N; i += 2)
s[i] = 2;
for (let i = 3; i <= N; i += 2) {
if (prime[i] == false) {
s[i] = i;
for (let j = i; j * i <= N; j += 2) {
if (prime[i * j] == false) {
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
function findSum(N)
{
let s = new Array(N + 1);
let ans = 1;
sieveOfEratosthenes(N, s);
let currFactor = s[N];
let power = 1;
while (N > 1) {
N = Math.floor(N/s[N]);
if (currFactor == s[N]) {
power++;
continue;
}
let sum = 0;
for(let i=0; i<=power; i++)
sum += Math.pow(currFactor,i);
ans *= sum;
currFactor = s[N];
power = 1;
}
return ans;
}
let n = 12;
document.write("Sum of the factors is : ");
document.write(findSum(n));
</script>
|
Output:
Sum of the factors is : 28
Time Complexity: O(N)
Auxiliary Space: O(n) // an extra n sized array has been created.
Last Updated :
20 Mar, 2023
Like Article
Save Article