Given a number N. The task is to find the sum of all the prime divisors of N.
Input: 60
Output: 10
2, 3, 5 are prime divisors of 60
Input: 39
Output: 16
3, 13 are prime divisors of 39
A naive approach will be to iterate for all numbers till N and check if the number divides N. If the number divides N, check if that number is prime or not. Add all the prime numbers till N which divides N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 1000005
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
int SumOfPrimeDivisors( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (n % i == 0) {
if (isPrime(i))
sum += i;
}
}
return sum;
}
int main()
{
int n = 60;
cout << "Sum of prime divisors of 60 is " << SumOfPrimeDivisors(n) << endl;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#define N 1000005
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
int SumOfPrimeDivisors( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (n % i == 0) {
if (isPrime(i))
sum += i;
}
}
return sum;
}
int main()
{
int n = 60;
printf ( "Sum of prime divisors of 60 is %d\n" ,SumOfPrimeDivisors(n));
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ;
i * i <= n; i = i + 6 )
if (n % i == 0 ||
n % (i + 2 ) == 0 )
return false ;
return true ;
}
static int SumOfPrimeDivisors( int n)
{
int sum = 0 ;
for ( int i = 1 ;
i <= n; i++)
{
if (n % i == 0 )
{
if (isPrime(i))
sum += i;
}
}
return sum;
}
public static void main(String args[])
{
int n = 60 ;
System.out.print( "Sum of prime divisors of 60 is " +
SumOfPrimeDivisors(n) + "\n" );
}
}
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false ;
return true ;
}
static int SumOfPrimeDivisors( int n)
{
int sum = 0;
for ( int i = 1;
i <= n; i++)
{
if (n % i == 0)
{
if (isPrime(i))
sum += i;
}
}
return sum;
}
public static void Main()
{
int n = 60;
Console.WriteLine( "Sum of prime divisors of 60 is " +
SumOfPrimeDivisors(n) + "\n" );
}
}
|
Javascript
<script>
let N = 1000005;
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function SumOfPrimeDivisors(n)
{
let sum = 0;
for (let i = 1; i <= n; i++) {
if (n % i == 0) {
if (isPrime(i))
sum += i;
}
}
return sum;
}
let n = 60;
document.write( "Sum of prime divisors of 60 is " +SumOfPrimeDivisors(n));
</script>
|
PHP
<?php
$N = 1000005;
function isPrime( $n )
{
global $N ;
if ( $n <= 1)
return false;
if ( $n <= 3)
return true;
if ( $n % 2 == 0 || $n % 3 == 0)
return false;
for ( $i = 5; $i * $i <= $n ;
$i = $i + 6)
if ( $n % $i == 0 ||
$n % ( $i + 2) == 0)
return false;
return true;
}
function SumOfPrimeDivisors( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $n % $i == 0)
{
if (isPrime( $i ))
$sum += $i ;
}
}
return $sum ;
}
$n = 60;
echo "Sum of prime divisors of 60 is " .
SumOfPrimeDivisors( $n );
?>
|
Python3
N = 1000005
def isPrime(n):
if n < = 1 :
return False
if n < = 3 :
return True
if n % 2 = = 0 or n % 3 = = 0 :
return False
i = 5
while i * i < = n:
if (n % i = = 0 or
n % (i + 2 ) = = 0 ):
return False
i = i + 6
return True
def SumOfPrimeDivisors(n):
sum = 0
for i in range ( 1 , n + 1 ) :
if n % i = = 0 :
if isPrime(i):
sum + = i
return sum
n = 60
print ( "Sum of prime divisors of 60 is " +
str (SumOfPrimeDivisors(n)))
|
Output
Sum of prime divisors of 60 is 10
Time Complexity: O(N * sqrt(N))
Efficient Approach: The complexity can be reduced using Sieve of Eratosthenes with some modifications. The modifications are as follows:
- Take an array of size N and substitute zero in all the indexes(initially consider all the numbers are prime).
- Iterate for all the numbers whose indexes have zero(i.e., it is prime numbers).
- Add this number to all it’s multiples less than N
- Return the array[N] value which has the sum stored in it.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int Sum( int N)
{
int SumOfPrimeDivisors[N+1] = { 0 };
for ( int i = 2; i <= N; ++i) {
if (!SumOfPrimeDivisors[i]) {
for ( int j = i; j <= N; j += i) {
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
int main()
{
int N = 60;
cout << "Sum of prime divisors of 60 is "
<< Sum(N) << endl;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int Sum( int N)
{
int SumOfPrimeDivisors[] = new int [N + 1 ];
for ( int i = 2 ; i <= N; ++i)
{
if (SumOfPrimeDivisors[i] == 0 )
{
for ( int j = i; j <= N; j += i)
{
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
public static void main(String args[])
{
int N = 60 ;
System.out.print( "Sum of prime " +
"divisors of 60 is " +
Sum(N) + "\n" );
}
}
|
C#
using System;
class GFG
{
static int Sum( int N)
{
int []SumOfPrimeDivisors = new int [N + 1];
for ( int i = 2; i <= N; ++i)
{
if (SumOfPrimeDivisors[i] == 0)
{
for ( int j = i;
j <= N; j += i)
{
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
public static void Main()
{
int N = 60;
Console.Write( "Sum of prime " +
"divisors of 60 is " +
Sum(N) + "\n" );
}
}
|
Javascript
<script>
function Sum(N)
{
let SumOfPrimeDivisors = new Array(N+1);
for (let i=0;i<SumOfPrimeDivisors.length;i++)
{
SumOfPrimeDivisors[i]=0;
}
for (let i = 2; i <= N; ++i)
{
if (SumOfPrimeDivisors[i] == 0)
{
for (let j = i; j <= N; j += i)
{
SumOfPrimeDivisors[j] += i;
}
}
}
return SumOfPrimeDivisors[N];
}
let N = 60;
document.write( "Sum of prime " +
"divisors of 60 is " +
Sum(N) + "<br>" );
</script>
|
PHP
<?php
function Sum( $N )
{
for ( $i = 0; $i <= $N ; $i ++)
$SumOfPrimeDivisors [ $i ] = 0;
for ( $i = 2; $i <= $N ; ++ $i )
{
if (! $SumOfPrimeDivisors [ $i ])
{
for ( $j = $i ; $j <= $N ; $j += $i )
{
$SumOfPrimeDivisors [ $j ] += $i ;
}
}
}
return $SumOfPrimeDivisors [ $N ];
}
$N = 60;
echo "Sum of prime divisors of 60 is " . Sum( $N );
?>
|
Python3
def Sum (N):
SumOfPrimeDivisors = [ 0 ] * (N + 1 )
for i in range ( 2 , N + 1 ) :
if (SumOfPrimeDivisors[i] = = 0 ) :
for j in range (i, N + 1 , i) :
SumOfPrimeDivisors[j] + = i
return SumOfPrimeDivisors[N]
N = 60
print ( "Sum of prime" ,
"divisors of 60 is" ,
Sum (N));
|
Output
Sum of prime divisors of 60 is 10
Time Complexity: O(N * log N)
Efficient Approach:
Time complexity can be reduced by finding all the factors efficiently. Below approach describe how to find all the factors efficiently. If we look carefully, all the divisors are present in pairs. For example if n = 100, then the various pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10) Using this fact we could speed up our program significantly. We, however, have to be careful if there are two equal divisors as in the case of (10, 10). In such case, we’d take only one of them.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
int SumOfPrimeDivisors( int n)
{
int sum = 0;
int root_n = ( int ) sqrt (n);
for ( int i = 1; i <= root_n; i++) {
if (n % i == 0) {
if (i == n / i && isPrime(i)) {
sum += i;
}
else {
if (isPrime(i)) {
sum += i;
}
if (isPrime(n / i)) {
sum += (n / i);
}
}
}
}
return sum;
}
int main()
{
int n = 60;
cout << "Sum of prime divisors of 60 is "
<< SumOfPrimeDivisors(n) << endl;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
int SumOfPrimeDivisors( int n)
{
int sum = 0;
int root_n = ( int ) sqrt (n);
for ( int i = 1; i <= root_n; i++) {
if (n % i == 0) {
if (i == n / i && isPrime(i)) {
sum += i;
}
else {
if (isPrime(i)) {
sum += i;
}
if (isPrime(n / i)) {
sum += (n / i);
}
}
}
}
return sum;
}
int main()
{
int n = 60;
printf ( "Sum of prime divisors of 60 is %d\n" ,SumOfPrimeDivisors(n));
}
|
Java
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static int SumOfPrimeDivisors( int n)
{
int sum = 0 ;
int root_n = ( int )Math.sqrt(n);
for ( int i = 1 ; i <= root_n; i++)
{
if (n % i == 0 )
{
if (i == n / i && isPrime(i))
{
sum += i;
}
else
{
if (isPrime(i))
{
sum += i;
}
if (isPrime(n / i))
{
sum += (n / i);
}
}
}
}
return sum;
}
public static void main(String[] args)
{
int n = 60 ;
System.out.println( "Sum of prime divisors of 60 is " +
SumOfPrimeDivisors(n));
}
}
|
C#
using System;
class GFG {
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static int SumOfPrimeDivisors( int n)
{
int sum = 0;
int root_n = ( int )Math.Sqrt(n);
for ( int i = 1; i <= root_n; i++) {
if (n % i == 0) {
if (i == n / i && isPrime(i)) {
sum += i;
}
else {
if (isPrime(i)) {
sum += i;
}
if (isPrime(n / i)) {
sum += (n / i);
}
}
}
}
return sum;
}
static void Main() {
int n = 60;
Console.WriteLine( "Sum of prime divisors of 60 is " + SumOfPrimeDivisors(n));
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function SumOfPrimeDivisors(n)
{
let sum = 0;
let root_n = parseInt(Math.sqrt(n), 10);
for (let i = 1; i <= root_n; i++) {
if (n % i == 0) {
if (i == parseInt(n / i, 10) && isPrime(i)) {
sum += i;
}
else {
if (isPrime(i)) {
sum += i;
}
if (isPrime(parseInt(n / i, 10))) {
sum += (parseInt(n / i, 10));
}
}
}
}
return sum;
}
let n = 60;
document.write( "Sum of prime divisors of 60 is "
+ SumOfPrimeDivisors(n) + "</br>" );
</script>
|
Python3
import math
def isPrime(n) :
if (n < = 1 ) :
return False
if (n < = 3 ) :
return True
if (n % 2 = = 0 or n % 3 = = 0 ) :
return False
i = 5
while i * i < = n :
if (n % i = = 0 or n % (i + 2 ) = = 0 ) :
return False
i = i + 6
return True
def SumOfPrimeDivisors(n) :
Sum = 0
root_n = ( int )(math.sqrt(n))
for i in range ( 1 , root_n + 1 ) :
if (n % i = = 0 ) :
if (i = = ( int )(n / i) and isPrime(i)) :
Sum + = i
else :
if (isPrime(i)) :
Sum + = i
if (isPrime(( int )(n / i))) :
Sum + = ( int )(n / i)
return Sum
n = 60
print ( "Sum of prime divisors of 60 is" , SumOfPrimeDivisors(n))
|
Output
Sum of prime divisors of 60 is 10
Time Complexity: O(sqrt(N) * sqrt(N))
Approach#4: Using filter, lambda, map
This approach defines two functions is_prime(n) and prime_divisor_sum(n) to determine whether a number is prime or not and to find the sum of all prime divisors of a number, respectively. The is_prime(n) function is used to filter out non-prime divisors using the filter() function inside prime_divisor_sum(n). Finally, the sum() function is used, to sum up all the prime divisors of the given number.
Algorithm
1. Define the function is_prime(n) to check if a number is prime or not. It returns True if the given number is prime, and False otherwise.
2. Define the function prime_divisor_sum(n) to find the sum of all prime divisors of the given number.
3. Use the filter() function inside prime_divisor_sum(n) to filter out all non-prime divisors from the range of 2 to n using a lambda function that checks if a number is a prime divisor of n.
4. Use the map() function to create an iterator that returns each element of the filtered iterator. The lambda function lambda x: x is used as the mapping function. It simply returns its input argument.
5. Finally, use the sum() function to sum up all the elements of the mapped iterator, giving the sum of all prime divisors of n.
Python3
def is_prime(n):
if n < = 1 :
return False
for i in range ( 2 , int (n * * 0.5 ) + 1 ):
if n % i = = 0 :
return False
return True
def prime_divisor_sum(n):
divisors = filter ( lambda x: n % x = = 0 and is_prime(x), range ( 2 , n + 1 ))
return sum ( map ( lambda x: x, divisors))
n = 60
print (prime_divisor_sum(n))
|
Time complexity: O(sqrt(n)), as it checks all numbers from 2 to sqrt(n) to see if n is divisible by any of them. The time complexity of the filter() function is also O(sqrt(n)), as it applies the lambda function to each number from 2 to n and returns an iterator of prime divisors. The time complexity of the map() function is O(k), where k is the number of elements in the filtered iterator. The time complexity of the sum() function is O(k), where k is the number of elements in the mapped iterator. Therefore, the overall time complexity of the code is O(sqrt(n) + k).
Space complexity: O(k), where k is the number of prime divisors of n, since we store them in the filtered and mapped iterators.
Python Solution(Using sympy):
This Python code utilizes the primefactors function from the sympy library to compute all prime factors of a given number n, then sums these factors to find the sum of its prime divisors.
Python3
from sympy import primefactors
def prime_divisor_sum(n):
prime_factors = primefactors(n)
return sum (prime_factors)
n = 60
print ( "Sum of prime divisors of" , n, "is" , prime_divisor_sum(n))
|
Sum of prime divisors of 60 is 10
Time Complexity (TC): The time complexity of this code depends on the efficiency of the primefactors function from the sympy library. Assuming this function has a time complexity of O(sqrt(n) log(log(n))), where n is the input number, the overall time complexity of the prime_divisor_sum function would be O(sqrt(n) log(log(n))).
Space Complexity (SC): The space complexity of this code is O(sqrt(n)) due to the storage of the prime factors returned by the primefactors function.
Contact Us