Prerequisite: Print all prime factors and their powers
Given natural numbers N and P, the task is to find the power of P in the factorization of N!.
Examples
Input: N = 4, P = 2
Output: 3
Explanation:
Power of 2 in the prime factorization of 4! = 24 is 3
Input: N = 24, P = 4
Output: 11
Naive Approach: The idea is to find the power of P for each number from 1 to N and add them as we know during multiplication power is added.
Time Complexity: O(N*P)
Efficient Approach:
To find the power of the number P in N! do the following:
- Find all the Prime Factors of the number P with their frequency by using the approach discussed in this article. Store the Prime Factors with their frequency in the map.
- Find the power of every Prime Factors of P in the factorization of N! by using the approach discussed in this article.
- Divide every power obtained in the above steps by their corresponding frequency in the map.
- Store the result of the above steps in an array, and a minimum of those elements will give the power of P in the factorization of N!.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unordered_map< int , int > Map;
void findPrimeFactors( int N)
{
int i;
Map.clear();
while (N % 2 == 0) {
Map[2] += 1;
N /= 2;
}
for (i = 3; i <= sqrt (N); i += 2) {
while (N % i == 0) {
Map[i] += 1;
N /= i;
}
}
if (N > 2) {
Map[N] += 1;
}
}
int PowInFactN( int N, int P)
{
int ans = 0;
int temp = P;
while (temp <= N) {
ans += N / temp;
temp = temp * P;
}
return ans;
}
int findPowers( int N, int P)
{
findPrimeFactors(P);
vector< int > Powers;
for ( auto & it : Map) {
int primeFac = it.first;
int facPow = it.second;
int p = PowInFactN(N,
primeFac);
p /= facPow;
Powers.push_back(p);
}
return *min_element(Powers.begin(),
Powers.end());
}
int main()
{
int N = 24, P = 4;
cout << findPowers(N, P);
return 0;
}
|
Java
import java.util.*;
class GFG{
static HashMap<Integer,Integer> Map = new HashMap<Integer,Integer>();
static void findPrimeFactors( int N)
{
int i;
Map.clear();
while (N % 2 == 0 ) {
if (Map.containsKey( 2 ))
Map.put( 2 , Map.get( 2 ) + 1 );
else
Map.put( 2 , 1 );
N /= 2 ;
}
for (i = 3 ; i <= Math.sqrt(N); i += 2 ) {
while (N % i == 0 ) {
if (Map.containsKey(i))
Map.put(i, Map.get(i) + 1 );
else
Map.put(i, 1 );
N /= i;
}
}
if (N > 2 ) {
if (Map.containsKey(N))
Map.put(N, Map.get(N) + 1 );
else
Map.put(N, 1 );
}
}
static int PowInFactN( int N, int P)
{
int ans = 0 ;
int temp = P;
while (temp <= N) {
ans += N / temp;
temp = temp * P;
}
return ans;
}
static int findPowers( int N, int P)
{
findPrimeFactors(P);
Vector<Integer> Powers = new Vector<Integer>();
for (Map.Entry<Integer, Integer> it : Map.entrySet()) {
int primeFac = it.getKey();
int facPow = it.getValue();
int p = PowInFactN(N,
primeFac);
p /= facPow;
Powers.add(p);
}
return Collections.min(Powers);
}
public static void main(String[] args)
{
int N = 24 , P = 4 ;
System.out.print(findPowers(N, P));
}
}
|
Python3
import math
Map = {}
def findPrimeFactors(N):
Map .clear()
while (N % 2 = = 0 ):
if 2 in Map :
Map [ 2 ] + = 1
else :
Map [ 2 ] = 1
N = N / / 2
for i in range ( 3 , int (math.sqrt(N)) + 1 , 2 ):
while (N % i = = 0 ):
if i in Map :
Map [i] + = 1
else :
Map [i] = 1
N = N / / i
if (N > 2 ):
if N in Map :
Map [N] + = 1
else :
Map [N] = 1
def PowInFactN(N, P):
ans = 0
temp = P
while (temp < = N):
ans = ans + (N / / temp)
temp = temp * P
return ans
def findPowers(N, P):
findPrimeFactors(P)
Powers = []
for it1, it2 in Map .items():
primeFac = it1
facPow = it2
p = PowInFactN(N, primeFac)
p = p / / facPow
Powers.append(p)
return min (Powers)
N, P = 24 , 4
if __name__ = = "__main__" :
print (findPowers(N, P))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
static Dictionary< int , int > Map = new Dictionary< int , int >();
static void findPrimeFactors( int N)
{
int i;
Map.Clear();
while (N % 2 == 0) {
if (Map.ContainsKey(2))
Map[2] = Map[2] + 1;
else
Map.Add(2, 1);
N /= 2;
}
for (i = 3; i <= Math.Sqrt(N); i += 2) {
while (N % i == 0) {
if (Map.ContainsKey(i))
Map[i] = Map[i] + 1;
else
Map.Add(i, 1);
N /= i;
}
}
if (N > 2) {
if (Map.ContainsKey(N))
Map[N] =Map[N] + 1;
else
Map.Add(N, 1);
}
}
static int PowInFactN( int N, int P)
{
int ans = 0;
int temp = P;
while (temp <= N) {
ans += N / temp;
temp = temp * P;
}
return ans;
}
static int findPowers( int N, int P)
{
findPrimeFactors(P);
List< int > Powers = new List< int >();
foreach (KeyValuePair< int , int > it in Map) {
int primeFac = it.Key;
int facPow = it.Value;
int p = PowInFactN(N,
primeFac);
p /= facPow;
Powers.Add(p);
}
return Powers.Min();
}
public static void Main(String[] args)
{
int N = 24, P = 4;
Console.Write(findPowers(N, P));
}
}
|
Javascript
let map = new Map();
function findPrimeFactors(N)
{
let i;
map.clear();
while (N % 2 == 0) {
if (map.has(2)){
map.set(2, map.get(2) + 1);
}
else {
map.set(2, 1);
}
N = Math.floor(N/2);
}
for (i = 3; i <= Math.sqrt(N); i += 2) {
while (N % i == 0) {
if (map.has(i)){
map.set(i, Map.get(i) + 1);
}
else {
map.set(i, 1);
}
N = Math.floor(N/i);
}
}
if (N > 2) {
if (map.has(N)){
map.set(N, map.get(N) + 1);
}
else {
map.set(N, 1);
}
}
}
function PowInFactN(N, P)
{
let ans = 0;
let temp = P;
while (temp <= N) {
ans = ans + Math.floor(N / temp);
temp = temp * P;
}
return ans;
}
function findPowers(N, P)
{
findPrimeFactors(P);
let Powers = new Array();
for (const [key,value] of map.entries()) {
let primeFac = key;
let facPow = value;
let p = PowInFactN(N, primeFac);
p = Math.floor(p/facPow);
Powers.push(p);
}
return Math.min(...Powers);
}
let N = 24;
let P = 4;
console.log(findPowers(N, P));
|
Time Complexity: O(sqrt(P)*(logP N))
Auxiliary Space: O(sqrt(P))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Jun, 2022
Like Article
Save Article