# Power of a prime number ‘r’ in n!

Given an integer n, find the power of a given prime number(r) in n!

**Examples : **

Input : n = 6 r = 3 Factorial of 6 is 720 = 2^4 * 3^2 *5 (prime factor 2,3,5) and power of 3 is 2 Output : 2 Input : n = 2000 r = 7 Output : 330

A simple method is to first calculate factorial of n, then factorize it to find the power of a prime number.

The above method can cause overflow for a slightly bigger numbers as factorial of a number is a big number. The idea is to consider prime factors of a factorial n.

**Legendre Factorization of n!**

For any prime number p and any integer n, let **Vp(n)** be the exponent of the largest power of p that divides n (that is, the p-adic valuation of n). Then

Vp(n) = summation of floor(n / p^i) and i goes from 1 to infinity

While the formula on the right side is an infinite sum, for any particular values of n and p it has only finitely many nonzero terms: for every i large enough that p^i > n, one has floor(n/p^i) = 0 . since, the sum is convergent.

Power of ‘r’ in n! = floor(n/r) + floor(n/r^2) + floor(n/r^3) + ....

Program to count power of a no. r in n! based on above formula.

## C++

`// C++ program to find power of ` `// a prime number ‘r’ in n! ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return power of a ` `// no. 'r' in factorial of n ` `int` `power(` `int` `n, ` `int` `r) ` `{ ` ` ` `// Keep dividing n by powers of ` ` ` `// 'r' and update count ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = r; (n / i) >= 1; i = i * r) ` ` ` `count += n / i; ` ` ` `return` `count; ` `} ` ` ` `// Driver program to ` `// test above function ` `int` `main() ` `{ ` ` ` `int` `n = 6, r = 3; ` ` ` `printf` `(` `" %d "` `, power(n, r)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find power of ` `// a prime number 'r' in n! ` ` ` `class` `GFG { ` ` ` `// Function to return power of a ` `// no. 'r' in factorial of n ` `static` `int` `power(` `int` `n, ` `int` `r) { ` ` ` ` ` `// Keep dividing n by powers of ` ` ` `// 'r' and update count ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = r; (n / i) >= ` `1` `; i = i * r) ` ` ` `count += n / i; ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `6` `, r = ` `3` `; ` ` ` `System.out.print(power(n, r)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python3 program to find power ` `# of a prime number ‘r’ in n! ` ` ` `# Function to return power of a ` `# no. 'r' in factorial of n ` `def` `power(n, r): ` ` ` ` ` `# Keep dividing n by powers of ` ` ` `# 'r' and update count ` ` ` `count ` `=` `0` `; i ` `=` `r ` ` ` ` ` `while` `((n ` `/` `i) >` `=` `1` `): ` ` ` `count ` `+` `=` `n ` `/` `i ` ` ` `i ` `=` `i ` `*` `r ` ` ` ` ` `return` `int` `(count) ` ` ` `# Driver Code ` `n ` `=` `6` `; r ` `=` `3` `print` `(power(n, r)) ` ` ` `# This code is contributed by Smitha Dinesh Semwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find power of ` `// a prime number 'r' in n! ` `using` `System; ` ` ` `class` `GFG { ` ` ` `// Function to return power of a ` `// no. 'r' in factorial of n ` `static` `int` `power(` `int` `n, ` `int` `r) { ` ` ` ` ` `// Keep dividing n by powers of ` ` ` `// 'r' and update count ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = r; (n / i) >= 1; i = i * r) ` ` ` `count += n / i; ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 6, r = 3; ` ` ` `Console.WriteLine(power(n, r)); ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP program to find power of ` `// a prime number ‘r’ in n! ` ` ` `// Function to return power of a ` `// no. 'r' in factorial of n ` `function` `power(` `$n` `, ` `$r` `) ` `{ ` ` ` ` ` `// Keep dividing n by powers ` ` ` `// of 'r' and update count ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= ` `$r` `; (` `$n` `/ ` `$i` `) >= 1; ` ` ` `$i` `= ` `$i` `* ` `$r` `) ` ` ` `$count` `+= ` `$n` `/ ` `$i` `; ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 6; ` `$r` `= 3; ` ` ` `echo` `power(` `$n` `, ` `$r` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Finding power of prime number p 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
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Highest power of a number that divides other number
- Sum of digits of a given number to a given power
- Find whether a given number is a power of 4 or not
- Check if a number is a power of another number
- GCD of a number raised to some power and another number
- Find the super power of a given Number
- Number of digits in 2 raised to power n
- Given a HUGE number check if it's a power of two.
- Highest power of 2 less than or equal to given number

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.