📜  求出从1到N的素因子的指数和

📅  最后修改于: 2021-04-21 21:50:31             🧑  作者: Mango

给定一个整数N ,任务是找到数字1到N的素因子的指数和。

例子:

方法:想法是使用素数因子及其幂的概念。步骤如下:

  1. 为每个数字从2N进行迭代,并对每个数字执行以下操作:
    1. 求出每个数N的素因子的幂。
    2. 在上述步骤中找到每个功率的总和
  2. 打印N的素数的所有幂的和并打印和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to implement sieve of
// erastosthenes
void sieveOfEratosthenes(int N, int s[])
{
    // Create a boolean array and
    // initialize all entries 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;
  
    // Iterate 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 generate prime
// factors and its power
int generatePrimeFactors(int N)
{
    // s[i] is going to store
    // smallest prime factor of i
    int s[N + 1];
    int sum = 0;
  
    sieveOfEratosthenes(N, s);
  
    // Current prime factor of N
    int curr = s[N];
  
    // Power of current prime factor
    int cnt = 1;
  
    // Calculating prime factors
    // and their powers sum
    while (N > 1) {
  
        N /= s[N];
  
        if (curr == s[N]) {
  
            // Increment the count and
            // continue the process
            cnt++;
            continue;
        }
  
        // Add count to the sum
        sum = sum + cnt;
  
        curr = s[N];
  
        // Reinitialize count
        cnt = 1;
    }
  
    // Return the result
    return sum;
}
  
// Function to find the sum of all the
// power of prime factors of N
void findSum(int N)
{
  
    int sum = 0;
  
    // Iterate for in [2, N]
    for (int i = 2; i <= N; i++) {
        sum += generatePrimeFactors(i);
    }
    cout << sum << endl;
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 4;
  
    // Function Call
    findSum(N);
    return 0;
}


Java
// Java program for the above approach 
class GFG{ 
  
// Function to implement sieve of 
// erastosthenes 
static void sieveOfEratosthenes(int N, int s[]) 
{ 
    // Create a boolean array and 
    // initialize all entries as false 
    boolean []prime = new boolean[N + 1];
  
    // Initializing smallest 
    // factor equal to 2 
    // for all the even numbers 
    for(int i = 2; i <= N; i += 2) 
        s[i] = 2; 
  
    // Iterate 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 generate prime 
// factors and its power 
static int generatePrimeFactors(int N) 
{ 
    // s[i] is going to store 
    // smallest prime factor of i 
    int []s = new int[N + 1]; 
    int sum = 0; 
  
    sieveOfEratosthenes(N, s); 
  
    // Current prime factor of N 
    int curr = s[N]; 
  
    // Power of current prime factor 
    int cnt = 1; 
  
    // Calculating prime factors 
    // and their powers sum 
    while (N > 1) 
    { 
        N /= s[N]; 
  
        if (curr == s[N])
        { 
  
            // Increment the count and 
            // continue the process 
            cnt++; 
            continue; 
        } 
  
        // Add count to the sum 
        sum = sum + cnt; 
  
        curr = s[N]; 
  
        // Reinitialize count 
        cnt = 1; 
    } 
  
    // Return the result 
    return sum; 
} 
  
// Function to find the sum of all the 
// power of prime factors of N 
static void findSum(int N) 
{ 
    int sum = 0; 
  
    // Iterate for in [2, N] 
    for(int i = 2; i <= N; i++)
    { 
        sum += generatePrimeFactors(i); 
    } 
    System.out.print(sum + "\n"); 
} 
  
// Driver Code 
public static void main(String[] args) 
{ 
    // Given Number N 
    int N = 4; 
  
    // Function Call 
    findSum(N); 
}
}
  
// This code is contributed by Ajay Kumar


Python3
# Python3 program for the above approach 
  
# Function to implement sieve of 
# erastosthenes 
def sieveOfEratosthenes(N, s):
  
    # Create a boolean array and 
    # initialize all entries 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
  
    # Iterate 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 
            j = i
            while (j * i <= N):
                if (prime[i * j] == False):
                    prime[i * j] = True
  
                    # i is the smallest 
                    # prime factor for 
                    # number "i*j" 
                    s[i * j] = i
  
                j += 2
  
# Function to generate prime 
# factors and its power 
def generatePrimeFactors(N):
  
    # s[i] is going to store 
    # smallest prime factor of i
    s = [0] * (N + 1)
    sum = 0
  
    sieveOfEratosthenes(N, s)
  
    # Current prime factor of N 
    curr = s[N]
  
    # Power of current prime factor 
    cnt = 1
  
    # Calculating prime factors 
    # and their powers sum 
    while (N > 1):
        N //= s[N]
          
        if (curr == s[N]):
  
            # Increment the count and 
            # continue the process 
            cnt += 1
            continue
  
        # Add count to the sum 
        sum = sum + cnt
  
        curr = s[N] 
  
        # Reinitialize count 
        cnt = 1
  
    # Return the result 
    return sum
  
# Function to find the sum of all the 
# power of prime factors of N 
def findSum (N):
  
    sum = 0
  
    # Iterate for in [2, N] 
    for i in range(2, N + 1):
        sum += generatePrimeFactors(i)
  
    print(sum)
  
# Driver Code
if __name__ == '__main__':
  
    # Given number N
    N = 4
      
    # Function call
    findSum(N)
  
# This code is contributed by himanshu77


C#
// C# program for the above approach 
using System;
  
class GFG{ 
  
// Function to implement sieve of 
// erastosthenes 
static void sieveOfEratosthenes(int N, int []s) 
{ 
      
    // Create a bool array and 
    // initialize all entries as false 
    bool []prime = new bool[N + 1];
      
    // Initializing smallest 
    // factor equal to 2 
    // for all the even numbers 
    for(int i = 2; i <= N; i += 2) 
        s[i] = 2; 
  
    // Iterate 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 generate prime 
// factors and its power 
static int generatePrimeFactors(int N) 
{ 
      
    // s[i] is going to store 
    // smallest prime factor of i 
    int []s = new int[N + 1]; 
    int sum = 0; 
  
    sieveOfEratosthenes(N, s); 
  
    // Current prime factor of N 
    int curr = s[N]; 
  
    // Power of current prime factor 
    int cnt = 1; 
  
    // Calculating prime factors 
    // and their powers sum 
    while (N > 1) 
    { 
        N /= s[N]; 
  
        if (curr == s[N])
        { 
              
            // Increment the count and 
            // continue the process 
            cnt++; 
            continue; 
        } 
  
        // Add count to the sum 
        sum = sum + cnt; 
  
        curr = s[N]; 
  
        // Reinitialize count 
        cnt = 1; 
    } 
  
    // Return the result 
    return sum; 
} 
  
// Function to find the sum of all the 
// power of prime factors of N 
static void findSum(int N) 
{ 
    int sum = 0; 
  
    // Iterate for in [2, N] 
    for(int i = 2; i <= N; i++)
    { 
        sum += generatePrimeFactors(i); 
    } 
    Console.Write(sum + "\n"); 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
      
    // Given number N 
    int N = 4; 
  
    // Function call 
    findSum(N); 
}
}
  
// This code is contributed by 29AjayKumar


输出:
4

时间复杂度: O(N * log 2 N)
辅助空间: O(N)