Finding power of prime number p in n!
Given a number ‘n’ and a prime number ‘p’. We need to find out the power of ‘p’ in the prime factorization of n!
Examples:
Input : n = 4, p = 2
Output : 3
Power of 2 in the prime factorization
of 2 in 4! = 24 is 3
Input : n = 24, p = 2
Output : 22
Naive approach
The naive approach is to find the power of p in each number from 1 to n and add them. Because we know that during multiplication power are added.
C/C++
// C++ implementation of finding // power of p in n! #include <bits/stdc++.h> using namespace std; // Returns power of p in n! int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // initializing int temp = p; // loop until temp<=n while (temp <= n) { // add number of numbers divisible by n ans += n / temp; // each time multiply temp by p temp = temp * p; } return ans; } // Driver function int main() { cout << PowerOFPINnfactorial(4, 2) << endl; return 0; } |
Java
// Java implementation of naive approach public class GFG { // Method to calculate the power of prime number p in n! static int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // finding power of p from 1 to n for (int i = 1; i <= n; i++) { // variable to note the power of p in i int count = 0, temp = i; // loop until temp is equal to i while (temp % p == 0) { count++; temp = temp / p; } // adding count to i ans += count; } return ans; } // Driver Method public static void main(String[] args) { System.out.println(PowerOFPINnfactorial(4, 2)); } } |
Python3
# Python3 implementation of # finding power of p in n! # Returns power of p in n! def PowerOFPINnfactorial(n, p): # initializing answer ans = 0; # initializing temp = p; # loop until temp<=n while (temp <= n): # add number of numbers # divisible by n ans += n / temp; # each time multiply # temp by p temp = temp * p; return ans; # Driver Code print(PowerOFPINnfactorial(4, 2)); # This code is contributed by # mits |
C#
// C# implementation of naive approach using System; public class GFG { // Method to calculate power // of prime number p in n! static int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // finding power of p from 1 to n for (int i = 1; i <= n; i++) { // variable to note the power of p in i int count = 0, temp = i; // loop until temp is equal to i while (temp % p == 0) { count++; temp = temp / p; } // adding count to i ans += count; } return ans; } // Driver Code public static void Main(String []args) { Console.WriteLine(PowerOFPINnfactorial(4, 2)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP implementation of // finding power of p in n! // Returns power of p in n! function PowerOFPINnfactorial($n, $p) { // initializing answer $ans = 0; // initializing $temp = $p; // loop until temp<=n while ($temp <= $n) { // add number of numbers // divisible by n $ans += $n / $temp; // each time multiply // temp by p $temp = $temp * $p; } return $ans; } // Driver Code echo PowerOFPINnfactorial(4, 2) . "\n"; // This code is contributed by // Akanksha Rai(Abby_akku) ?> |
Output:
3
Efficient Approach
Before discussing efficient approach lets discuss a question given a two numbers n, m how many numbers are there from 1 to n that are divisible by m the answer is floor(n/m).
Now coming back to our original question to find the power of p in n! what we do is count the number of numbers from 1 to n that are divisible by p then by
then by
till
> n and add them. This will be our required answer.
Powerofp(n!) = floor(n/p) + floor(n/p^2) + floor(n/p^3)........
Below is the implementation of the above steps.
C/C++
// C++ implementation of finding power of p in n! #include <bits/stdc++.h> using namespace std; // Returns power of p in n! int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // initializing int temp = p; // loop until temp<=n while (temp <= n) { // add number of numbers divisible by n ans += n / temp; // each time multiply temp by p temp = temp * p; } return ans; } // Driver function int main() { cout << PowerOFPINnfactorial(4, 2) << endl; return 0; } |
Java
// Java implementation of finding power of p in n! public class GFG { // Method to calculate the power of prime number p in n! static int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // initializing int temp = p; // loop until temp<=n while (temp <= n) { // add number of numbers divisible by n ans += n / temp; // each time multiply temp by p temp = temp * p; } return ans; } // Driver Method public static void main(String[] args) { System.out.println(PowerOFPINnfactorial(4, 2)); } } |
Python3
# Python3 implementation of # finding power of p in n! # Returns power of p in n! def PowerOFPINnfactorial(n, p): # initializing answer ans = 0 # initializing temp = p # loop until temp<=n while (temp <= n) : # add number of numbers # divisible by n ans += n / temp # each time multiply # temp by p temp = temp * p return int(ans) # Driver Code print(PowerOFPINnfactorial(4, 2)) # This code is contributed # by Smitha |
C#
// C# implementation of finding // power of p in n! using System; public class GFG { // Method to calculate power // of prime number p in n! static int PowerOFPINnfactorial(int n, int p) { // initializing answer int ans = 0; // initializing int temp = p; // loop until temp <= n while (temp <= n) { // add number of numbers divisible by n ans += n / temp; // each time multiply temp by p temp = temp * p; } return ans; } // Driver Code public static void Main(String []args) { Console.WriteLine(PowerOFPINnfactorial(4, 2)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP implementation of // finding power of p in n! // Returns power of p in n! function PowerOFPINnfactorial($n, $p) { // initializing answer $ans = 0; // initializing $temp = $p; // loop until temp<=n while ($temp <= $n) { // add number of numbers divisible by n $ans += $n / $temp; // each time multiply temp by p $temp = $temp * $p; } return $ans; } // Driver function echo PowerOFPINnfactorial(4, 2) . "\n"; // This code is contributed // by Akanksha Rai(Abby_akku) ?> |
Output:
3
Time Complexity :O(
(n))
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Recommended Posts:
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Power of a prime number ‘r’ in n!
- Find power of power under mod of a prime
- Largest power of k in n! (factorial) where k may not be prime
- Check if given number is a power of d where d is a power of 2
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Finding number of digits in n'th Fibonacci number
- Finding sum of digits of a number until sum becomes single digit
- Highest power of a number that divides other number
- Find whether a given number is a power of 4 or not
- Sum of digits of a given number to a given power
- GCD of a number raised to some power and another number
- Check if a number is a power of another number



