📜  一个数字的所有主要除数之和

📅  最后修改于: 2021-04-29 07:06:08             🧑  作者: Mango

给定数字N。任务是找到N的所有素数之和。

例子:

Input: 60
Output: 10
2, 3, 5 are prime divisors of 60

Input: 39
Output: 16
3, 13 are prime divisors of 39

天真的方法是对所有数字进行迭代直到N,然后检查数字是否除以N。如果数字除以N,请检查该数字是否为质数。将所有质数相加,直到N除以N。

下面是上述方法的实现:

C++
// CPP program to find sum of
// prime divisors of N
#include 
using namespace std;
#define N 1000005
  
// Function to check if the
// number is prime or not.
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}
  
// function to find sum of prime
// divisors of N
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;
}
// Driver code
int main()
{
    int n = 60;
    cout << "Sum of prime divisors of 60 is " << SumOfPrimeDivisors(n) << endl;
}


Java
// Java program to find sum 
// of prime divisors of N
import java.io.*;
import java.util.*;
  
class GFG
{
// Function to check if the
// number is prime or not.
static boolean isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that
    // we can skip middle five
    // numbers in below loop
    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;
}
  
// function to find 
// sum of prime
// divisors of N
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;
}
  
// Driver code
public static void main(String args[])
{
    int n = 60;
    System.out.print("Sum of prime divisors of 60 is " + 
                          SumOfPrimeDivisors(n) + "\n");
}
}


C#
// C# program to find sum 
// of prime divisors of N
using System;
class GFG
{
      
// Function to check if the
// number is prime or not.
static bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that
    // we can skip middle five
    // numbers in below loop
    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;
}
  
// function to find 
// sum of prime
// divisors of N
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;
}
  
// Driver code
public static void Main()
{
    int n = 60;
    Console.WriteLine("Sum of prime divisors of 60 is " + 
                        SumOfPrimeDivisors(n) + "\n");
}
}
  
// This code is contributed
// by inder_verma.


Python 3
# Python 3 program to find 
# sum of prime divisors of N
N = 1000005
  
# Function to check if the
# number is prime or not.
def isPrime(n):
      
    # Corner cases
    if n <= 1:
        return False
    if n <= 3:
        return True
  
    # This is checked so that  
    # we can skip middle five 
    # numbers in below loop
    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
  
# function to find sum 
# of prime divisors of N
def SumOfPrimeDivisors(n):
    sum = 0
    for i in range(1, n + 1) :
        if n % i == 0 :
            if isPrime(i):
                sum += i
      
    return sum
  
# Driver code
n = 60
print("Sum of prime divisors of 60 is " + 
              str(SumOfPrimeDivisors(n)))
  
# This code is contributed
# by ChitraNayal


PHP


C++
// CPP program to find prime divisors of
// all numbers from 1 to n
#include 
using namespace std;
  
// function to find prime divisors of
// all numbers from 1 to n
int Sum(int N)
{
    int SumOfPrimeDivisors[N+1] = { 0 };
  
    for (int i = 2; i <= N; ++i) {
  
        // if the number is prime
        if (!SumOfPrimeDivisors[i]) {
  
            // add this prime to all it's multiples
            for (int j = i; j <= N; j += i) {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
    return SumOfPrimeDivisors[N];
}
  
// Driver code
int main()
{
    int N = 60;
    cout << "Sum of prime divisors of 60 is "
         << Sum(N) << endl;
}


Java
// Java program to find
// prime divisors of
// all numbers from 1 to n
import java.io.*;
import java.util.*;
  
class GFG
{
      
// function to find prime 
// divisors of all numbers 
// from 1 to n
static int Sum(int N)
{
    int SumOfPrimeDivisors[] = new int[N + 1];
      
  
    for (int i = 2; i <= N; ++i) 
    {
  
        // if the number is prime
        if (SumOfPrimeDivisors[i] == 0) 
        {
  
            // add this prime to
            // all it's multiples
            for (int j = i; j <= N; j += i) 
            {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
    return SumOfPrimeDivisors[N];
}
  
// Driver code
public static void main(String args[])
{
    int N = 60;
    System.out.print("Sum of prime " + 
                "divisors of 60 is " + 
                       Sum(N) + "\n");
}
}


Python3
# Python 3 program to find
# prime divisors of
# all numbers from 1 to n
  
# function to find prime 
# divisors of all numbers 
# from 1 to n
def Sum(N):
   
    SumOfPrimeDivisors = [0] * (N + 1)
       
    for i in range(2, N + 1) :
       
        # if the number is prime
        if (SumOfPrimeDivisors[i] == 0) :
           
            # add this prime to
            # all it's multiples
            for j in range(i, N + 1, i) :
               
                SumOfPrimeDivisors[j] += i
               
    return SumOfPrimeDivisors[N]
   
# Driver code
N = 60
print("Sum of prime" , 
      "divisors of 60 is", 
                  Sum(N));
                    
# This code is contributed 
# by Smitha


C#
// C# program to find
// prime divisors of
// all numbers from 1 to n
using System;
  
class GFG
{
      
// function to find prime 
// divisors of all numbers 
// from 1 to n
static int Sum(int N)
{
    int []SumOfPrimeDivisors = new int[N + 1];
      
    for (int i = 2; i <= N; ++i) 
    {
  
        // if the number is prime
        if (SumOfPrimeDivisors[i] == 0) 
        {
  
            // add this prime to
            // all it's multiples
            for (int j = i; 
                     j <= N; j += i) 
            {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
      
    return SumOfPrimeDivisors[N];
}
  
// Driver code
public static void Main()
{
    int N = 60;
    Console.Write("Sum of prime " + 
                    "divisors of 60 is " + 
                         Sum(N) + "\n");
}
}
  
// This code is contributed
// by Smitha


PHP


输出:
Sum of prime divisors of 60 is 10

时间复杂度: O(N * sqrt(N))

高效的方法:使用Eratosthenes筛网并进行一些修改可以降低复杂性。修改如下:

  • 采取大小为N的数组,并在所有索引中替换零(最初考虑所有数字均为质数)。
  • 迭代索引为零的所有数字(即,它是质数)。
  • 将此数字加到小于N的所有倍数上
  • 返回其中存储了总和的array [N]值。

下面是上述方法的实现。

C++

// CPP program to find prime divisors of
// all numbers from 1 to n
#include 
using namespace std;
  
// function to find prime divisors of
// all numbers from 1 to n
int Sum(int N)
{
    int SumOfPrimeDivisors[N+1] = { 0 };
  
    for (int i = 2; i <= N; ++i) {
  
        // if the number is prime
        if (!SumOfPrimeDivisors[i]) {
  
            // add this prime to all it's multiples
            for (int j = i; j <= N; j += i) {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
    return SumOfPrimeDivisors[N];
}
  
// Driver code
int main()
{
    int N = 60;
    cout << "Sum of prime divisors of 60 is "
         << Sum(N) << endl;
}

Java

// Java program to find
// prime divisors of
// all numbers from 1 to n
import java.io.*;
import java.util.*;
  
class GFG
{
      
// function to find prime 
// divisors of all numbers 
// from 1 to n
static int Sum(int N)
{
    int SumOfPrimeDivisors[] = new int[N + 1];
      
  
    for (int i = 2; i <= N; ++i) 
    {
  
        // if the number is prime
        if (SumOfPrimeDivisors[i] == 0) 
        {
  
            // add this prime to
            // all it's multiples
            for (int j = i; j <= N; j += i) 
            {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
    return SumOfPrimeDivisors[N];
}
  
// Driver code
public static void main(String args[])
{
    int N = 60;
    System.out.print("Sum of prime " + 
                "divisors of 60 is " + 
                       Sum(N) + "\n");
}
}

Python3

# Python 3 program to find
# prime divisors of
# all numbers from 1 to n
  
# function to find prime 
# divisors of all numbers 
# from 1 to n
def Sum(N):
   
    SumOfPrimeDivisors = [0] * (N + 1)
       
    for i in range(2, N + 1) :
       
        # if the number is prime
        if (SumOfPrimeDivisors[i] == 0) :
           
            # add this prime to
            # all it's multiples
            for j in range(i, N + 1, i) :
               
                SumOfPrimeDivisors[j] += i
               
    return SumOfPrimeDivisors[N]
   
# Driver code
N = 60
print("Sum of prime" , 
      "divisors of 60 is", 
                  Sum(N));
                    
# This code is contributed 
# by Smitha

C#

// C# program to find
// prime divisors of
// all numbers from 1 to n
using System;
  
class GFG
{
      
// function to find prime 
// divisors of all numbers 
// from 1 to n
static int Sum(int N)
{
    int []SumOfPrimeDivisors = new int[N + 1];
      
    for (int i = 2; i <= N; ++i) 
    {
  
        // if the number is prime
        if (SumOfPrimeDivisors[i] == 0) 
        {
  
            // add this prime to
            // all it's multiples
            for (int j = i; 
                     j <= N; j += i) 
            {
  
                SumOfPrimeDivisors[j] += i;
            }
        }
    }
      
    return SumOfPrimeDivisors[N];
}
  
// Driver code
public static void Main()
{
    int N = 60;
    Console.Write("Sum of prime " + 
                    "divisors of 60 is " + 
                         Sum(N) + "\n");
}
}
  
// This code is contributed
// by Smitha

的PHP


输出:
Sum of prime divisors of 60 is 10

时间复杂度: O(N * log N)