📜  一个数的所有素数之和 |设置 2

📅  最后修改于: 2022-05-13 01:56:06.371000             🧑  作者: Mango

一个数的所有素数之和 |设置 2

给定一个数N ,任务是找到N的所有质因数之和。

例子:

方法:这个问题可以通过找出数的所有质因数来解决。请按照以下步骤解决此问题:

  • 将变量sum初始化为0以存储N的素数除数之和。
  • 如果N可以被2 整除,则将2加到sum中,然后将N除以2直到它可以被整除。
  • 使用变量i[3, sqrt(N)]范围内迭代,增量为2
    • 如果N可以被i 整除,则将i加到sum中,然后将N除以i直到它可以被整除。
  • 如果N是大于2 的素数,则将N加到sum 中。
  • 完成上述步骤后,打印总和作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出
7

时间复杂度: O(sqrt(N))
辅助空间: O(1)