Sum of largest divisible powers of p (a prime number) in a range
Last Updated :
29 Mar, 2024
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.
Native Approach : Run a loop from L to R. For every number, check if it is divisible by P. If yes, then find the largest power of P that divides current number. If yes, then add this value to result.
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfDivisblePowers( int L, int R, int P) {
int res = 0;
for ( int i = L; i <= R; i++)
{
int x = i;
while (x % P == 0)
{
res++;
x /= P;
}
}
return res;
}
int main() {
int L = 1, R = 10, P = 2;
cout << sumOfDivisblePowers(L, R, P);
return 0;
}
|
Java
class GFG
{
static int sumOfDivisblePowers( int L, int R, int P)
{
int res = 0 ;
for ( int i = L; i <= R; i++)
{
int x = i;
while (x % P == 0 )
{
res++;
x /= P;
}
}
return res;
}
public static void main (String[] args)
{
int L = 1 , R = 10 , P = 2 ;
System.out.println(sumOfDivisblePowers(L, R, P));
}
}
|
Python3
def sumOfDivisblePowers(L, R, P):
res = 0
for i in range (L, R + 1 ):
x = i
while (x % P = = 0 ):
res + = 1
x / = P
return res
L = 1
R = 10
P = 2
print (sumOfDivisblePowers(L, R, P))
|
C#
using System;
class GFG
{
static int sumOfDivisblePowers( int L, int R, int P)
{
int res = 0;
for ( int i = L; i <= R; i++)
{
int x = i;
while (x % P == 0)
{
res++;
x /= P;
}
}
return res;
}
public static void Main ()
{
int L = 1, R = 10, P = 2;
Console.WriteLine(sumOfDivisblePowers(L, R, P));
}
}
|
PHP
<?php
function sumOfDivisblePowers( $L , $R , $P )
{
$res = 0;
for ( $i = $L ; $i <= $R ; $i ++)
{
$x = $i ;
while ( $x % $P == 0)
{
$res ++;
$x /= $P ;
}
}
return $res ;
}
$L = 1;
$R = 10;
$P = 2;
echo sumOfDivisblePowers( $L , $R , $P );
?>
|
Javascript
<script>
function sumOfDivisblePowers(L, R, P)
{
let res = 0;
for (let i = L; i <= R; i++)
{
let x = i;
while (x % P == 0)
{
res++;
x = parseInt(x / P, 10);
}
}
return res;
}
let L = 1, R = 10, P = 2;
document.write(sumOfDivisblePowers(L, R, P));
</script>
|
Efficient approach: The idea is to use to Legendre’s formula.
The largest possible power of P that divides factorial of a number x is ?x/p? + ?x/(p2)? + ?x/(p3)? + ……
Using above, we can find the largest power of P that divides R! (or 1 * 2 * 3…. R).
Sum of largest powers from L to R = Largest Power of P that divides R! (or 1 * 2 … L-1 * L * … R) – Largest Power of P that divides (L-1)! (or 1 * 2 … L-1)
C++
#include <bits/stdc++.h>
using namespace std;
int largestPower( int x, int P)
{
int res = 0;
while (x)
{
x /= P;
res += x;
}
return res;
}
int sumOfDivisblePowers( int L, int R, int P) {
return largestPower(R, P) - largestPower(L-1, P);
}
int main() {
int L = 1, R = 10, P = 2;
cout << sumOfDivisblePowers(L, R, P);
return 0;
}
|
Java
class GFG
{
static int largestPower( int x, int P)
{
int res = 0 ;
while (x != 0 )
{
x /= P;
res += x;
}
return res;
}
static int sumOfDivisblePowers( int L, int R, int P)
{
return largestPower(R, P) - largestPower(L- 1 , P);
}
public static void main (String[] args)
{
int L = 1 , R = 10 , P = 2 ;
System.out.println(sumOfDivisblePowers(L, R, P));
}
}
|
Python3
def largestPower(x, P):
res = 0
while (x):
x = int (x / P)
res + = x
return res
def sumOfDivisblePowers( L, R, P) :
return largestPower(R, P) - largestPower(L - 1 , P)
L = 1
R = 10
P = 2
print (sumOfDivisblePowers(L, R, P))
|
C#
using System;
class GFG {
static int largestPower( int x, int P)
{
int res = 0;
while (x>0)
{
x /= P;
res += x;
}
return res;
}
static int sumOfDivisblePowers( int L,
int R, int P)
{
return largestPower(R, P)
- largestPower(L-1, P);
}
static void Main()
{
int L = 1, R = 10, P = 2;
Console.Write(
sumOfDivisblePowers(L, R, P));
}
}
|
PHP
<?php
function largestPower( $x , $P )
{
$res = 0;
while ( $x )
{
$x = floor ( $x / $P );
$res += $x ;
}
return $res ;
}
function sumOfDivisblePowers( $L , $R , $P ) {
return largestPower( $R , $P ) - largestPower( $L -1, $P );
}
$L = 1;
$R = 10;
$P = 2;
echo sumOfDivisblePowers( $L , $R , $P );
?>
|
Javascript
<script>
function largestPower(x, P)
{
let res = 0;
while (x>0)
{
x = parseInt(x / P, 10);
res += x;
}
return res;
}
function sumOfDivisblePowers(L, R, P)
{
return largestPower(R, P) - largestPower(L-1, P);
}
let L = 1, R = 10, P = 2;
document.write(sumOfDivisblePowers(L, R, P));
</script>
|
Share your thoughts in the comments
Please Login to comment...