

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Power of a prime number r in n! in C++
<p style="">In this problem, we are given two integer n and r. Our task is to find the power of the given prime number r in the factorial of the number n.</p><p>Let’s take an example to understand the problem</p><p><strong>Input</strong> − n = 6 r = 2</p><p><strong>Output</strong> − 4</p><p style=""><strong>Explanation</strong> −</p><pre class="result notranslate" style="">Factorial n, 6! = 6*5*4*3*2*1 = 720 720 = 2<sup>4</sup> * 3<sup>2</sup> * 5, power of 2 is 4</pre><p>To solve this problem, a simple solution would be directly finding the factorial and then finding the power of the prime number. But this is not the best solution.</p><p>Another efficient solution is using a formula,</p><p>Power of ‘r’ in n! = floor(n/r) + floor(n/r2) + floor(n/r3) + ...</p><h2>Example</h2><p>Program to show the implementation of our solution,</p><p><a class="demo" href="http://tpcg.io/4PJVxPLd" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">#include <iostream> using namespace std; int primePower(int n, int r) { int count = 0; for (int i = r; (n / i) >= 1; i = i * r) count = count+n/i; return count; } int main() { int n = 6, r = 2; cout<<"Power of prime number "<<r<<"in factorial "<<n<<" is : "<<primePower(n, r); return 0; }</pre><h2>Output</h2><pre class="result notranslate">Power of prime number 2in factorial 6 is : 4</pre>
- Related Questions & Answers
- Finding the power of prime number p in n! in C++
- Primitive root of a prime number n modulo n in C++
- Find power of power under mod of a prime in C++
- How to find prime factors of a number in R?
- Program for power of a complex number in O(log n) in C++
- Number of digits in 2 raised to power n in C++
- Kth prime number greater than N in C++
- How to find the fractional power of a negative number in R?
- Calculating power of a number in MySQL?
- Check whether N is a Dihedral Prime Number or not in Python
- Nearest prime less than given number n C++
- Print the nearest prime number formed by adding prime numbers to N
- Prime factors of a big number in C++
- Prime factor array of a Number in JavaScript
- Prime digits sum of a number in JavaScript
Advertisements