Find maximum value of x such that n ! % ( k ^ x ) = 0 | C ++ program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class GfG { public : int findX ( int n , int k ) { int r = n , v , u ; int m = sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = min ( r , u / v ) ; } } return r ; } } ; int main ( ) { GfG g ; int n = 5 ; int k = 2 ; cout << g . findX ( n , k ) ; }