- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find maximum value of x such that n! % (k^x) = 0 in C++

Suppose we have two integers n and k. We have to find the maximum value of x, such that n! mod (k^x) = 0. So when n = 5, and k = 2, then output will be 3. As n! = 120, now for different values of x, it will be −

120 mod 2^0 = 0, 120 mod 2^1 = 0, 120 mod 2^2 = 0, 120 mod 2^3 = 0, 120 mod 2^4 = 8, 120 mod 2^5 = 24, 120 mod 2^6 = 56, 120 mod 2^7 = 120. As the max value of x = 3, the result is 0, so the output is 3.

To solve this, we have to follow these steps −

- Take the square root of k, and store it into m
- For i := 2 to m, do the following steps:
- When i = m, then set i := k
- if k is divisible by i, then, divide k by i
- Run a loop to n, and add quotient to a variable called u.
- Store the min value of r, after each loop

## Example

#include <iostream> #include <cmath> using namespace std; int calculateMaxX(int n, int k) { int result = 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; } result = min(result, u / v); } } return result; } int main() { int n = 5; int k = 2; cout<<"Maximum value of x is: " << calculateMaxX(n, k); }

## Output

Maximum value of x is: 3

- Related Questions & Answers
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Find smallest values of x and y such that ax – by = 0 in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Maximize the value of x + y + z such that ax + by + cz = n in C++
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Find the Number of Solutions of n = x + n x using C++
- Count of all possible values of X such that A % X = B in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Find the smallest number X such that X! contains at least Y trailing zeros in C++

Advertisements