Given P and N, Find The Largest X Such That P X Divides N!
Given P and N, Find The Largest X Such That P X Divides N!
divides n!
Given an integer n and a prime number p, find the largest x such that p x (p raised to power x)
divides n! (factorial)
Examples:
Input: n = 7, p = 3
Output: x = 2
32 divides 7! and 2 is the largest such power of 3.
Input: n = 10, p = 3
Output: x = 4
34 divides 10! and 4 is the largest such power of 3.
We strongly recommend to minimize your browser and try this yourself first.
n! is multiplication of {1, 2, 3, 4, n}.
How many numbers in {1, 2, 3, 4, .. n} are divisible by p?
Every pth number is divisible by p in {1, 2, 3, 4, .. n}. Therefore in n!, there are n/p numbers
divisible by p. So we know that the value of x (largest power of p that divides n!) is at-least n/p .
Can x be larger than n/p ?
Yes, there may be numbers which are divisible by p2, p3,
How many numbers in {1, 2, 3, 4, .. n} are divisible by p 2, p3, ?
There are n/(p2) numbers divisible by p2 (Every p2th number would be divisible). Similarly, there
are n/(p3) numbers divisible by p3 and so on.
What is the largest possible value of x?
So the largest possible power is n/p + n/(p2) + n/(p3) +
Note that we add only n/(p2) only once (not twice) as one p is already considered by expression
n/p. Similarly, we consider n/(p3) (not thrice).
Following is C implementation based on above idea.
// C program to find largest x such that p*x divides n!
#include <stdio.h>
// Returns largest power of p that divides n!
int largestPower(int n, int p)
{
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n)
{
n /= p;
x += n;
}
return x;
}
// Driver program
int main()
{
int n = 10, p = 3;
printf("The largest power of %d that divides %d! is %d\n",
p, n, largestPower(n, p));
return 0;
}
Input: n = 50
Output: 14
Input: n = 328
Output: 60
This problem is mainly a variation of previous article on Compute sum of digits in all numbers
from 1 to n.
We strongly recommend you to minimize your browser and try this yourself
first.
Naive Solution:
A naive solution is to go through every number x from 1 to n, and check if x has 4. To check if x
has or not, we can traverse all digits of x. Below is C++ implementation of this idea.
// A Simple C++ program to compute sum of digits in numbers from 1 to n
#include<iostream>
using namespace std;
bool has4(int x);
// Returns sum of all digits in numbers from 1 to n
int countNumbersWith4(int n)
{
int result = 0; // initialize result
return result;
Efficient Solution:
Above is a naive solution. We can do it more efficiently by finding a pattern.
Let us take few examples.
Count of numbers from 0 to 9 = 1
Count of numbers from 0 to 99 = 1*9 + 10 = 19
Count of numbers from 0 to 999 = 19*9 + 100 = 271
In below implementation, the above formula is implemented using dynamic programming as there
are overlapping subproblems.
The above formula is one core step of the idea. Below is complete algorithm.
1) Find number of digits minus one in n. Let this value be 'd'.
For 328, d is 2.
// Computing 10^d
int p = ceil(pow(10, d));
// Most significant digit (msd) of n,
// For 328, msd is 3 which can be obtained using 328/100
int msd = n/p;
//
//
//
//
if
//
//
//
//
//
//
if
Input: n = 12
Output: Sum of digits in numbers from 1 to 12 = 51
Input: n = 328
Output: Sum of digits in numbers from 1 to 328 = 3241
Naive Solution:
A naive solution is to go through every number x from 1 to n, and compute sum in x by traversing
all digits of x. Below is C++ implementation of this idea.
// A Simple C++ program to compute sum of digits in numbers from 1 to n
#include<iostream>
using namespace std;
int sumOfDigits(int );
// Returns sum of all digits in numbers from 1 to n
int sumOfDigitsFrom1ToN(int n)
{
int result = 0; // initialize result
// One by one compute sum of digits in every number from
// 1 to n
for (int x=1; x<=n; x++)
result += sumOfDigits(x);
return result;
}
// A utility function to compute sum of digits in a
// given number x
int sumOfDigits(int x)
{
int sum = 0;
while (x != 0)
{
sum += x %10;
x
= x /10;
}
return sum;
}
// Driver Program
int main()
{
int n = 328;
cout << "Sum of digits in numbers from 1 to " << n << " is "
<< sumOfDigitsFrom1ToN(n);
return 0;
}
Run on IDE
Output
Sum of digits in numbers from 1 to 328 is 3241
Efficient Solution:
Above is a naive solution. We can do it more efficiently by finding a pattern.
Let us take few examples.
sum(9) = 1 + 2 + 3 + 4 ........... + 9
= 9*10/2
= 45
programming as
300 to 328.
For 328, this sum is computed as 3*29 + recursive call "sum(28)"
In general, this sum can be computed as msd * (n % (msd*10 d) + 1)
+ sum(n % (10d))
// Driver Program
int main()
{
int n = 328;
cout << "Sum of digits in numbers from 1 to " << n << " is "
<< sumOfDigitsFrom1ToN(n);
return 0;
}
Run on IDE
Output