Find sum of exponents of prime factors of numbers 1 to N - GeeksforGeeks

25 Mar, 2022
Given an integer N, the task is to find the sum of exponents of prime factors of numbers 1 to N.
Examples:
Input: N = 4Output: 4Explanation:Numbers up to 4 are 1, 2, 3, 4 whereThe exponent of 1 in the prime factorization of 1 is 0 (20), For 2 it is 1 (21), For 3 it is 1 (31), and For 4 it is 2 (22).The sum of the exponent of prime factors of each number up to 4 is 0 + 1 + 1 + 2 = 4.
Input: N = 10Output: 15Explanation:sum of the exponent of prime factors of each number up to 10 is 15.
Approach: The idea is to use the concept of Prime factors and their powers. Below are the steps:
Iterate for each number from 2 to N and for each number do the following:find the power of prime factors for each number N.Find the summation of each power in the above stepsPrint the summation of all the powers of prime factors of N and print the sum.
Iterate for each number from 2 to N and for each number do the following:find the power of prime factors for each number N.Find the summation of each power in the above steps
find the power of prime factors for each number N.Find the summation of each power in the above steps
find the power of prime factors for each number N.
Find the summation of each power in the above steps
Print the summation of all the powers of prime factors of N and print the sum.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to implement sieve of// eratosthenesvoid sieveOfEratosthenes(int N, int s[]){    // Create a boolean array and    // initialize all entries as false    vector<bool> 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 powerint 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 Nvoid 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 Codeint main(){    // Given Number N    int N = 4;     // Function Call    findSum(N);    return 0;}
// Java program for the above approachclass GFG{ // Function to implement sieve of// eratosthenesstatic 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 powerstatic 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 Nstatic 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 Codepublic static void main(String[] args){    // Given Number N    int N = 4;     // Function Call    findSum(N);}} // This code is contributed by Ajay Kumar
# Python3 program for the above approach # Function to implement sieve of# eratosthenesdef 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 powerdef 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 Ndef findSum (N):     sum = 0     # Iterate for in [2, N]    for i in range(2, N + 1):        sum += generatePrimeFactors(i)     print(sum) # Driver Codeif __name__ == '__main__':     # Given number N    N = 4         # Function call    findSum(N) # This code is contributed by himanshu77
// C# program for the above approachusing System; class GFG{ // Function to implement sieve of// eratosthenesstatic 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 powerstatic 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 Nstatic 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 Codepublic static void Main(String[] args){         // Given number N    int N = 4;     // Function call    findSum(N);}} // This code is contributed by 29AjayKumar
<script> // Javascript program for the above approach  // Function to implement sieve of// eratosthenesfunction sieveOfEratosthenes(N, s){    // Create a boolean array and    // initialize all entries as false    let prime = Array.from({length: N+1}, (_, i) => 0);     // Initializing smallest    // factor equal to 2    // for all the even numbers    for(let i = 2; i <= N; i += 2)        s[i] = 2;     // Iterate for odd numbers    // less then equal to n    for(let 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(let 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 powerfunction generatePrimeFactors(N){    // s[i] is going to store    // smallest prime factor of i    let s = Array.from({length: N+1}, (_, i) => 0);    let sum = 0;     sieveOfEratosthenes(N, s);     // Current prime factor of N    let curr = s[N];     // Power of current prime factor    let 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 Nfunction findSum(N){    let sum = 0;     // Iterate for in [2, N]    for(let i = 2; i <= N; i++)    {        sum += generatePrimeFactors(i);    }    document.write(sum + "\n");} // Driver Code         // Given Number N    let N = 4;     // Function Call    findSum(N);         </script>
4
 
Time Complexity: O(N*log2N)Auxiliary Space: O(N)
29AjayKumar
himanshu77
target_2
sumitgumber28
factor
maths-power
number-theory
Numbers
Prime Number
prime-factor
Greedy
Mathematical
number-theory
Greedy
Mathematical
Prime Number
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Huffman Coding | Greedy Algo-3
Coin Change | DP-7
Fractional Knapsack Problem
Activity Selection Problem | Greedy Algo-1
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays