Skip to content
Related Articles
Open in App
Not now

Related Articles

Sum of all the prime divisors of a number | Set 2

Improve Article
Save Article
Like Article
  • Last Updated : 06 Jul, 2021
Improve Article
Save Article
Like Article

Given a number N, the task is to find the sum of all the prime factors of N

Examples:

Input: 10
Output: 7
Explanation: 2, 5 are prime divisors of 10

Input: 20
Output: 7
Explanation: 2, 5 are prime divisors of 20

Approach: This problem can be solved by finding all the prime factors of the number. Follow the steps below to solve this problem:

  • Initialize a variable sum as 0 to store the sum of prime divisors of N.
  • If N is divisible by 2, add 2 to sum and divide N by 2 until it is divisible.
  • Iterate in the range [3, sqrt(N)] using the variable i, with an increment of 2:
    • If N is divisible by i, add i to sum and divide N by i until it is divisible.
  • If N is a prime number greater than 2, add N to sum.
  • After completing the above steps, print the sum as the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find sum of prime
// divisors of the given number N
int SumOfPrimeDivisors(int n)
{
 
    int sum = 0;
 
    // Add the number 2 if it divides N
    if (n % 2 == 0) {
        sum = sum + 2;
    }
 
    while (n % 2 == 0) {
        n = n / 2;
    }
 
    // Traverse the loop from [3, sqrt(N)]
    for (int i = 3; i <= sqrt(n); i = i + 2) {
 
        // If i divides N, add i and divide N
        if (n % i == 0) {
            sum = sum + i;
        }
 
        while (n % i == 0) {
            n = n / i;
        }
    }
 
    // This condition is to handle the case when N
    // is a prime number greater than 2
    if (n > 2) {
        sum = sum + n;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    // Given Input
    int n = 10;
 
    // Function Call
    cout << SumOfPrimeDivisors(n);
    return 0;
}

Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
  // Function to find sum of prime
  // divisors of the given number N
  public static int SumOfPrimeDivisors(int n)
  {
 
    int sum = 0;
 
    // Add the number 2 if it divides N
    if (n % 2 == 0) {
      sum = sum + 2;
    }
 
    while (n % 2 == 0) {
      n = n / 2;
    }
 
    // Traverse the loop from [3, sqrt(N)]
    for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
 
      // If i divides N, add i and divide N
      if (n % i == 0) {
        sum = sum + i;
      }
 
      while (n % i == 0) {
        n = n / i;
      }
    }
 
    // This condition is to handle the case when N
    // is a prime number greater than 2
    if (n > 2) {
      sum = sum + n;
    }
 
    return sum;
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    // Given Input
    int n = 10;
 
    // Function Call
    System.out.println(SumOfPrimeDivisors(n));
  }
}
 
// This code is contributed by Potta Lokesh

Python3




# Python3 program for the above approach
import math
 
# Function to find sum of prime
# divisors of the given number N
def SumOfPrimeDivisors(n):
     
    sum = 0
     
    # Add the number 2 if it divides N
    if n % 2 == 0:
        sum += 2
         
    while n % 2 == 0:
        n //= 2
         
    # Traverse the loop from [3, sqrt(N)]
    k = int(math.sqrt(n))
     
    for i in range(3, k + 1, 2):
         
        # If i divides N, add i and divide N
        if n % i == 0:
            sum += i
             
        while n % i == 0:
            n //= i
             
    # This condition is to handle the case when N
    # is a prime number greater than 2
    if n > 2:
        sum += n
         
    # Return the sum
    return sum
 
# Driver code
if __name__ == '__main__':
     
    # Given input
    n = 10
     
    # Function call
    print(SumOfPrimeDivisors(n))
     
# This code is contributed by MuskanKalra1

C#




// C# program for the above approach
 
using System;
 
class GFG {
  // Function to find sum of prime
  // divisors of the given number N
  public static int SumOfPrimeDivisors(int n)
  {
 
    int sum = 0;
 
    // Add the number 2 if it divides N
    if (n % 2 == 0) {
      sum = sum + 2;
    }
 
    while (n % 2 == 0) {
      n = n / 2;
    }
 
    // Traverse the loop from [3, sqrt(N)]
    for (int i = 3; i <= Math.Sqrt(n); i = i + 2) {
 
      // If i divides N, add i and divide N
      if (n % i == 0) {
        sum = sum + i;
      }
 
      while (n % i == 0) {
        n = n / i;
      }
    }
 
    // This condition is to handle the case when N
    // is a prime number greater than 2
    if (n > 2) {
      sum = sum + n;
    }
 
    return sum;
  }
 
  // Driver code
  public static void Main (String[] args)
  {
 
    // Given Input
    int n = 10;
 
    // Function Call
    Console.Write(SumOfPrimeDivisors(n));
  }
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
        // JavaScript program for the above approach
 
        // Function to find sum of prime
        // divisors of the given number N
        function SumOfPrimeDivisors(n) {
 
            let sum = 0;
 
            // Add the number 2 if it divides N
            if (n % 2 == 0) {
                sum = sum + 2;
            }
 
            while (n % 2 == 0) {
                n = n / 2;
            }
 
            // Traverse the loop from [3, sqrt(N)]
            for (let i = 3; i <= Math.sqrt(n); i = i + 2) {
 
                // If i divides N, add i and divide N
                if (n % i == 0) {
                    sum = sum + i;
                }
 
                while (n % i == 0) {
                    n = n / i;
                }
            }
 
            // This condition is to handle the case when N
            // is a prime number greater than 2
            if (n > 2) {
                sum = sum + n;
            }
 
            return sum;
        }
 
        // Driver code
 
        // Given Input
        let n = 10;
 
        // Function Call
        document.write(SumOfPrimeDivisors(n));
 
// This code is contributed by Potta Lokesh
 
    </script>

Output

7

Time complexity: O(sqrt(N))
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!