Given an integer N, the task is to find the sum of exponents of prime factors of numbers 1 to N.
Examples:
Input: N = 4
Output: 4
Explanation:
Numbers up to 4 are 1, 2, 3, 4 where
The 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 = 10
Output: 15
Explanation:
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 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++
#include <bits/stdc++.h>
using namespace std;
void sieveOfEratosthenes( int N, int s[])
{
vector< bool > prime(N + 1, false );
for ( int i = 2; i <= N; i += 2)
s[i] = 2;
for ( int i = 3; i <= N; i += 2) {
if (prime[i] == false ) {
s[i] = i;
for ( int j = i;
j * i <= N; j += 2) {
if (prime[i * j] == false ) {
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
int generatePrimeFactors( int N)
{
int s[N + 1];
int sum = 0;
sieveOfEratosthenes(N, s);
int curr = s[N];
int cnt = 1;
while (N > 1) {
N /= s[N];
if (curr == s[N]) {
cnt++;
continue ;
}
sum = sum + cnt;
curr = s[N];
cnt = 1;
}
return sum;
}
void findSum( int N)
{
int sum = 0;
for ( int i = 2; i <= N; i++) {
sum += generatePrimeFactors(i);
}
cout << sum << endl;
}
int main()
{
int N = 4;
findSum(N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static void sieveOfEratosthenes( int N, int s[])
{
boolean []prime = new boolean [N + 1 ];
for ( int i = 2 ; i <= N; i += 2 )
s[i] = 2 ;
for ( int i = 3 ; i <= N; i += 2 )
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i;
j * i <= N; j += 2 )
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static int generatePrimeFactors( int N)
{
int []s = new int [N + 1 ];
int sum = 0 ;
sieveOfEratosthenes(N, s);
int curr = s[N];
int cnt = 1 ;
while (N > 1 )
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
sum = sum + cnt;
curr = s[N];
cnt = 1 ;
}
return sum;
}
static void findSum( int N)
{
int sum = 0 ;
for ( int i = 2 ; i <= N; i++)
{
sum += generatePrimeFactors(i);
}
System.out.print(sum + "\n" );
}
public static void main(String[] args)
{
int N = 4 ;
findSum(N);
}
}
|
Python3
def sieveOfEratosthenes(N, s):
prime = [ False ] * (N + 1 )
for i in range ( 2 , N + 1 , 2 ):
s[i] = 2
for i in range ( 3 , N + 1 , 2 ):
if (prime[i] = = False ):
s[i] = i
j = i
while (j * i < = N):
if (prime[i * j] = = False ):
prime[i * j] = True
s[i * j] = i
j + = 2
def generatePrimeFactors(N):
s = [ 0 ] * (N + 1 )
sum = 0
sieveOfEratosthenes(N, s)
curr = s[N]
cnt = 1
while (N > 1 ):
N / / = s[N]
if (curr = = s[N]):
cnt + = 1
continue
sum = sum + cnt
curr = s[N]
cnt = 1
return sum
def findSum (N):
sum = 0
for i in range ( 2 , N + 1 ):
sum + = generatePrimeFactors(i)
print ( sum )
if __name__ = = '__main__' :
N = 4
findSum(N)
|
C#
using System;
class GFG{
static void sieveOfEratosthenes( int N, int []s)
{
bool []prime = new bool [N + 1];
for ( int i = 2; i <= N; i += 2)
s[i] = 2;
for ( int i = 3; i <= N; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i;
j * i <= N; j += 2)
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static int generatePrimeFactors( int N)
{
int []s = new int [N + 1];
int sum = 0;
sieveOfEratosthenes(N, s);
int curr = s[N];
int cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
sum = sum + cnt;
curr = s[N];
cnt = 1;
}
return sum;
}
static void findSum( int N)
{
int sum = 0;
for ( int i = 2; i <= N; i++)
{
sum += generatePrimeFactors(i);
}
Console.Write(sum + "\n" );
}
public static void Main(String[] args)
{
int N = 4;
findSum(N);
}
}
|
Javascript
<script>
function sieveOfEratosthenes(N, s)
{
let prime = Array.from({length: N+1}, (_, i) => 0);
for (let i = 2; i <= N; i += 2)
s[i] = 2;
for (let i = 3; i <= N; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for (let j = i;
j * i <= N; j += 2)
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
function generatePrimeFactors(N)
{
let s = Array.from({length: N+1}, (_, i) => 0);
let sum = 0;
sieveOfEratosthenes(N, s);
let curr = s[N];
let cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
sum = sum + cnt;
curr = s[N];
cnt = 1;
}
return sum;
}
function findSum(N)
{
let sum = 0;
for (let i = 2; i <= N; i++)
{
sum += generatePrimeFactors(i);
}
document.write(sum + "\n" );
}
let N = 4;
findSum(N);
</script>
|
Time Complexity: O(N*log2N)
Auxiliary Space: O(N)