Finding power of prime number p in n ! | C ++ implementation of finding power of p in n ! ; Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } int main ( ) { cout << PowerOFPINnfactorial ( 4 , 2 ) << endl ; return 0 ; }