Finding powers of any number P in N ! | C # program to find the power of P in N ! ; Map to store all the prime factors of P ; Function to find the prime factors of N im Map ; Clear map ; Check for factors of 2 ; Find all the prime factors ; If i is a factors then increase the frequency of i ; Function to find the power of prime number P in N ! ; Loop until temp <= N ; Add the number of numbers divisible by N ; Each time multiply temp by P ; Returns ans ; Function that find the powers of any P in N ! ; Find all prime factors of number P ; To store the powers of all prime factors ; Traverse the map ; Prime factor and corres . powers ; Find power of prime factor primeFac ; Divide frequency by facPow ; Store the power of primeFac ^ facPow ; Return the minimum element in Power array ; Driver 's Code ; Function to find power of P in N !
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 ) ) ; } }