Count prime factors of N!
Given an integer N, the task is to count the number of prime factors of N!.
Examples:
Input: N = 5
Output: 3
Explanation: Factorial of 5 = 120. Prime factors of 120 are {2, 3, 5}. Therefore, the count is 3.
Input: N = 1
Output: 0
Naive Approach: Follow the steps to solve the problem :
- Initialize a variable, say fac, to store the factorial of a number.
- Initialize a variable, say count, to count the prime factors of N!.
- Iterate over the range [2, fac], and if the number is not prime, increment count.
- Print the count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int factorial( int f)
{
if (f == 0 || f == 1) {
return 1;
}
else {
return (f * factorial(f - 1));
}
}
bool isPrime( int element)
{
for ( int i = 2;
i <= sqrt (element); i++) {
if (element % i == 0) {
return false ;
}
}
return true ;
}
int countPrimeFactors( int N)
{
int fac = factorial(N);
int count = 0;
for ( int i = 2; i <= fac; i++) {
if (fac % i == 0 && isPrime(i)) {
count++;
}
}
cout << count;
}
int main()
{
int N = 5;
countPrimeFactors(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int factorial( int f)
{
if (f == 0 || f == 1 ) {
return 1 ;
}
else {
return (f * factorial(f - 1 ));
}
}
static boolean isPrime( int element)
{
for ( int i = 2 ;
i <= ( int )Math.sqrt(element); i++) {
if (element % i == 0 ) {
return false ;
}
}
return true ;
}
static void countPrimeFactors( int N)
{
int fac = factorial(N);
int count = 0 ;
for ( int i = 2 ; i <= fac; i++) {
if ((fac % i == 0 && isPrime(i))) {
count++;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int N = 5 ;
countPrimeFactors(N);
}
}
|
Python3
from math import sqrt
def factorial(f):
if (f = = 0 or f = = 1 ):
return 1
else :
return (f * factorial(f - 1 ))
def isPrime(element):
for i in range ( 2 , int (sqrt(element)) + 1 ):
if (element % i = = 0 ):
return False
return True
def countPrimeFactors(N):
fac = factorial(N)
count = 0
for i in range ( 2 , fac + 1 ):
if (fac % i = = 0 and isPrime(i)):
count + = 1
print (count)
N = 5
countPrimeFactors(N)
|
C#
using System;
class GFG
{
static int factorial( int f)
{
if (f == 0 || f == 1) {
return 1;
}
else {
return (f * factorial(f - 1));
}
}
static bool isPrime( int element)
{
for ( int i = 2;
i <= ( int )Math.Sqrt(element); i++) {
if (element % i == 0) {
return false ;
}
}
return true ;
}
static void countPrimeFactors( int N)
{
int fac = factorial(N);
int count = 0;
for ( int i = 2; i <= fac; i++) {
if ((fac % i == 0 && isPrime(i))) {
count++;
}
}
Console.Write(count);
}
public static void Main()
{
int N = 5;
countPrimeFactors(N);
}
}
|
Javascript
<script>
function factorial(f){
if (f == 0 || f == 1){
return 1
}
else {
return (f * factorial(f - 1))
}
}
function isPrime(element){
for (let i = 2; i < Math.floor(Math.sqrt(element)+1); i++){
if (element % i == 0){
return false
}
}
return true
}
function countPrimeFactors(N){
let fac = factorial(N)
let count = 0
for (let i = 2; i < fac + 1; i++){
if (fac % i == 0 && isPrime(i)){
count += 1
}
}
document.write(count)
}
let N = 5
countPrimeFactors(N)
</script>
|
Time Complexity: O(N! * sqrt(N))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Sieve Of Eratosthenes. Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the count of prime factors of N!.
- Initialize a boolean array, say prime[] to check if a number is prime or not.
- Perform Sieve of Eratosthenes and populate count at each iteration, if found prime.
- Print the value of count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPrimeFactors( int N)
{
int count = 0;
bool prime[N + 1];
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= N; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p <= N; p++) {
if (prime[p]) {
count++;
}
}
cout << count;
}
int main()
{
int N = 5;
countPrimeFactors(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void countPrimeFactors( int N)
{
int count = 0 ;
boolean [] prime = new boolean [N + 1 ];
Arrays.fill(prime, true );
for ( int p = 2 ; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= N; i += p)
prime[i] = false ;
}
}
for ( int p = 2 ; p <= N; p++) {
if (prime[p] != false ) {
count++;
}
}
System.out.print(count);
}
public static void main(String[] args)
{
int N = 5 ;
countPrimeFactors(N);
}
}
|
Python3
def countPrimeFactors(N):
count = 0
prime = [ 1 ] * (N + 1 )
for p in range ( 2 , N + 1 ):
if p * p > N:
break
if (prime[p]):
for i in range (p * p, N + 1 , p):
prime[i] = 0
for p in range ( 2 , N + 1 ):
if (prime[p]):
count + = 1
print (count)
if __name__ = = '__main__' :
N = 5
countPrimeFactors(N)
|
C#
using System;
public class GFG
{
static void countPrimeFactors( int N)
{
int count = 0;
bool [] prime = new bool [N + 1];
for ( int i = 0; i < prime.Length; i++)
prime[i] = true ;
for ( int p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= N; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p <= N; p++) {
if (prime[p] != false ) {
count++;
}
}
Console.Write(count);
}
public static void Main(String[] args)
{
int N = 5;
countPrimeFactors(N);
}
}
|
Javascript
<script>
function countPrimeFactors( N) {
var count = 0;
var prime = Array(N + 1).fill( true );
for ( var p = 2; p * p <= N; p++) {
if (prime[p] == true ) {
for ( var i = p * p; i <= N; i += p)
prime[i] = false ;
}
}
for ( var p = 2; p <= N; p++) {
if (prime[p] != false ) {
count++;
}
}
document.write(count);
}
var N = 5;
countPrimeFactors(N);
</script>
|
Time Complexity: O(N * log(logN))
Auxiliary Space: O(N)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...