Sum of largest divisible powers of p (a prime number) in a range

Given a range [L, R], and a prime number P. We are required to find the sum of the highest power of P in all numbers from L to R.

Examples:

Input : L = 1, R = 10, P = 2
Output : 8
There are 10 integers in the range, and:
In 1 the highest power of 2 is 0
In 2 the highest power of 2 is 1
In 3 the highest power of 2 is 0
Similarly the highest powers of 2 in 4,
5, 6, 7, 8, 9, 10 are 2, 0, 1, 0, 3,
0, 1 respectively.
Input : L = 10, R = 20, P = 7
Output : 1
There are 11 integers in the range, and:
In 10 the highest power of 7 is 0
In 11 the highest power of 7 is 0
In 12 the highest power of 7 is 0
Similarly the highest power of 7 in 13,
14, 15, 16, 17, 18, 19, 20 and 10 is 0,
1, 0, 0, 0, 0, 0 and 0 respectively.

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.