Prerequisite: Print all prime factors and their powers
Given natural numbers N and P, the task is to find the power of P in the factorization of N!.
Input: N = 4, P = 2
Power of 2 in the prime factorization of 4! = 24 is 3
Input: N = 24, P = 4
Naive Approach: The idea is to find the power of P for each number from 1 to N and add them as we know during multiplication power is added.
Time Complexity: O(N*P)
To find the power of the number P in N! do the following:
- Find all the Prime Factors of the number P with their frequency by using the approach discussed in this article. Store the Prime Factors with their frequency in map.
- Find the power of every Prime Factors of P in the factorization of N! by using the approach discussed in this article.
- Divide the every power obtained in the above steps by their corresponding frequency in the map.
- Store the result of above steps in an array and minimum of those element will give the power of P in the factorisation of N!.
Below is the implementation of the above approach:
Time Complexity: O(sqrt(P)*(logP N))
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of given powers of 2 required to represent a number
- Representation of a number in powers of other
- Check if a number can be represented as sum of non zero powers of 2
- Find all powers of 2 less than or equal to a given number
- Distinct powers of a number N such that the sum is equal to K
- Balance pans using given weights that are powers of a number
- Form a number using corner digits of powers
- Number of triangles possible with given lengths of sticks which are powers of 2
- Minimum cost to form a number X by adding up powers of 2
- Sum of largest divisible powers of p (a prime number) in a range
- Sum of first N natural numbers by taking powers of 2 as negative number
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Finding number of digits in n'th Fibonacci number
- Finding the Parity of a number Efficiently
- Finding power of prime number p in n!
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Finding sum of digits of a number until sum becomes single digit
- Finding number of days between two dates using StringStream
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Check if a number is perfect square without finding square root
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.