Sum of Factors of a Number using Prime Factorization (original) (raw)
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:
C++ `
// C++ Program to find sum of all // factors of a given number
#include <bits/stdc++.h>
using namespace std;
// Using SieveOfEratosthenes to find smallest prime // factor of all the numbers. // For example, if N is 10, // s[2] = s[4] = s[6] = s[10] = 2 // s[3] = s[9] = 3 // s[5] = 5 // s[7] = 7 void sieveOfEratosthenes(int N, int s[]) { // Create a boolean array "prime[0..n]" and // initialize all entries in it as false. vector prime(N + 1, false);
// Initializing smallest factor equal to 2
// for all the even numbers
for (int i = 2; i <= N; i += 2)
s[i] = 2;
// For odd numbers less then equal to n
for (int i = 3; i <= N; i += 2) {
if (prime[i] == false) {
// s(i) for a prime is the number itself
s[i] = i;
// For all multiples of current prime number
for (int j = i; j * i <= N; j += 2) {
if (prime[i * j] == false) {
prime[i * j] = true;
// i is the smallest prime factor for
// number "i*j".
s[i * j] = i;
}
}
}
}}
// Function to find sum of all prime factors int findSum(int N) { // Declaring array to store smallest prime // factor of i at i-th index int s[N + 1];
int ans = 1;
// Filling values in s[] using sieve
sieveOfEratosthenes(N, s);
int currFactor = s[N]; // Current prime factor of N
int power = 1; // Power of current prime factor
while (N > 1) {
N /= s[N];
// N is now N/s[N]. If new N also has smallest
// prime factor as currFactor, increment power
if (currFactor == s[N]) {
power++;
continue;
}
int sum = 0;
for(int i=0; i<=power; i++)
sum += pow(currFactor,i);
ans *= sum;
// Update current prime factor as s[N] and
// initializing power of factor as 1.
currFactor = s[N];
power = 1;
}
return ans;}
// Driver code int main() { int n = 12;
cout << "Sum of the factors is : ";
cout << findSum(n);
return 0;}
Java
//Java Program to find sum of all //factors of a given number public class GFG {
//Using SieveOfEratosthenes to find smallest prime
//factor of all the numbers.
//For example, if N is 10,
//s[2] = s[4] = s[6] = s[10] = 2
//s[3] = s[9] = 3
//s[5] = 5
//s[7] = 7
static void sieveOfEratosthenes(int N, int s[])
{
// Create a boolean array "prime[0..n]" and
// initialize all entries in it as false.
boolean[] prime = new boolean[N + 1];
for(int i = 0; i < N+1; i++)
prime[i] = false;
// Initializing smallest factor equal to 2
// for all the even numbers
for (int i = 2; i <= N; i += 2)
s[i] = 2;
// For odd numbers less then equal to n
for (int i = 3; i <= N; i += 2) {
if (prime[i] == false) {
// s(i) for a prime is the number itself
s[i] = i;
// For all multiples of current prime number
for (int j = i; j * i <= N; j += 2) {
if (prime[i * j] == false) {
prime[i * j] = true;
// i is the smallest prime factor for
// number "i*j".
s[i * j] = i;
}
}
}
}
}
//Function to find sum of all prime factors
static int findSum(int N)
{
// Declaring array to store smallest prime
// factor of i at i-th index
int[] s = new int[N + 1];
int ans = 1;
// Filling values in s[] using sieve
sieveOfEratosthenes(N, s);
int currFactor = s[N]; // Current prime factor of N
int power = 1; // Power of current prime factor
while (N > 1) {
N /= s[N];
// N is now N/s[N]. If new N also has smallest
// prime factor as currFactor, increment power
if (currFactor == s[N]) {
power++;
continue;
}
int sum = 0;
for(int i=0; i<=power; i++)
sum += Math.pow(currFactor,i);
ans *= sum;
// Update current prime factor as s[N] and
// initializing power of factor as 1.
currFactor = s[N];
power = 1;
}
return ans;
}
//Driver code
public static void main(String[] args) {
int n = 12;
System.out.print("Sum of the factors is : ");
System.out.print(findSum(n));
}}
Python 3
Python 3 Program to find
sum of all factors of a
given number
Using SieveOfEratosthenes
to find smallest prime
factor of all the numbers.
For example, if N is 10,
s[2] = s[4] = s[6] = s[10] = 2
s[3] = s[9] = 3
s[5] = 5
s[7] = 7
def sieveOfEratosthenes(N, s) :
# Create a boolean list "prime[0..n]"
# and initialize all entries in it
# as false.
prime = [False] * (N + 1)
# Initializing smallest
# factor equal to 2 for
# all the even numbers
for i in range(2, N + 1, 2) :
s[i] = 2
# For odd numbers less
# then equal to n
for i in range(3, N + 1, 2) :
if prime[i] == False :
# s[i] for a prime is
# the number itself
s[i] = i
# For all multiples of
# current prime number
for j in range(i, (N + 1) // i, 2) :
if prime[i * j] == False :
prime[i * j] = True
# i is the smallest
# prime factor for
# number "i*j".
s[i * j] = i
#J += 2Function to find sum
of all prime factors
def findSum(N) :
# Declaring list to store
# smallest prime factor of
# i at i-th index
s = [0] * (N + 1)
ans = 1
# Filling values in s[] using
# sieve function calling
sieveOfEratosthenes(N, s)
# Current prime factor of N
currFactor = s[N]
# Power of current prime factor
power = 1
while N > 1 :
N //= s[N]
# N is now N//s[N]. If new N
# also has smallest prime
# factor as currFactor,
# increment power
if currFactor == s[N] :
power += 1
continue
sum = 0
for i in range(power + 1) :
sum += pow(currFactor, i)
ans *= sum
# Update current prime factor
# as s[N] and initializing
# power of factor as 1.
currFactor = s[N]
power = 1
return ans Driver Code
if name == "main" :
n = 12
print("Sum of the factors is :", end = " ")
print(findSum(n))This code is contributed by ANKITRAI1
C#
// C# Program to find sum of all // factors of a given number using System;
class GFG {
// Using SieveOfEratosthenes to find smallest // prime factor of all the numbers. // For example, if N is 10, // s[2] = s[4] = s[6] = s[10] = 2 // s[3] = s[9] = 3 // s[5] = 5 // s[7] = 7 static void sieveOfEratosthenes(int N, int []s) {
// Create a boolean array "prime[0..n]" and // initialize all entries in it as false. bool[] prime = new bool[N + 1];
for(int i = 0; i < N + 1; i++) prime[i] = false;
// Initializing smallest factor equal // to 2 for all the even numbers for (int i = 2; i <= N; i += 2) s[i] = 2;
// For odd numbers less then equal to n for (int i = 3; i <= N; i += 2) { if (prime[i] == false) {
// s(i) for a prime is the
// number itself
s[i] = i;
// For all multiples of current
// prime number
for (int j = i; j * i <= N; j += 2)
{
if (prime[i * j] == false)
{
prime[i * j] = true;
// i is the smallest prime factor
// for number "i*j".
s[i * j] = i;
}
}
}} }
// Function to find sum of all // prime factors static int findSum(int N) { // Declaring array to store smallest // prime factor of i at i-th index int[] s = new int[N + 1];
int ans = 1;
// Filling values in s[] using sieve sieveOfEratosthenes(N, s);
int currFactor = s[N]; // Current prime factor of N int power = 1; // Power of current prime factor
while (N > 1) { N /= s[N];
// N is now N/s[N]. If new N also has smallest
// prime factor as currFactor, increment power
if (currFactor == s[N])
{
power++;
continue;
}
int sum = 0;
for(int i = 0; i <= power; i++)
sum += (int)Math.Pow(currFactor, i);
ans *= sum;
// Update current prime factor as s[N]
// and initializing power of factor as 1.
currFactor = s[N];
power = 1;}
return ans; }
// Driver code public static void Main() { int n = 12;
Console.Write("Sum of the factors is : ");
Console.WriteLine(findSum(n));} }
// This code is contributed by Shashank
PHP
JavaScript
`
Output:
Sum of the factors is : 28
Time Complexity: O(N)
Auxiliary Space: O(n) // an extra n sized array has been created.