Given two numbers k and n, find the largest power of k that divides n!
K > 1
Input : n = 7, k = 2 Output : 4 Explanation : 7! = 5040 The largest power of 2 that divides 5040 is 24. Input : n = 10, k = 9 Output : 2 The largest power of 9 that divides 10! is 92.
We have discussed a solution in below post when k is always prime.
Now to find the power of any non-prime number k in n!, we first find all the prime factors of the number k along with the count of number of their occurrences. Then for each prime factor, we count occurrences using Legendre’s formula which states that the largest possible power of a prime number p in n is ⌊n/p⌋ + ⌊n/(p2)⌋ + ⌊n/(p3)⌋ + ……
Over all the prime factors p of K, the one with the minimum value of findPowerOfK(n, p)/count will be our answer where count is number of occurrences of p in k.
This article is contributed by ShivamKD. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the last digit when factorial of A divides factorial of B
- Find power of power under mod of a prime
- Find maximum power of a number that divides a factorial
- Number of digits in N factorial to the power N
- Check if N is a Factorial Prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Check if a subarray of length K with sum equal to factorial of a number exists or not
- Largest number not greater than N which can become prime after rearranging its digits
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Check whether the sum of prime elements of the array is prime or not
- Largest subset where absolute difference of any two element is a power of 2
- Finding power of prime number p in n!
- Power of a prime number ‘r’ in n!
- Elements of Array which can be expressed as power of prime numbers
- Check if the number is a Prime power number
- Count of distinct power of prime factor of N
Improved By : 29AjayKumar