Open In App

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.

 



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. 
 




// Simple CPP Program to find sum of largest
// divisible powers of P in [L, R]
#include <bits/stdc++.h>
using namespace std;
  
int sumOfDivisblePowers(int L, int R, int P) {
  
   // Traverse through all numbers from L to R
   int res = 0;
   for (int i = L; i <= R; i++)
   {
      // If P divides current number x, add 
      // largest power of P that divides x.
      int x = i;
      while (x % P == 0)
      {
         res++;
         x  /= P;
      }
   }
  
   return res;
}
  
// Driver code
int main() {
  int L = 1, R = 10, P = 2;
  cout << sumOfDivisblePowers(L, R, P);
  return 0;
}




// Simple Java Program to find sum of largest
// divisible powers of P in [L, R]
  
class GFG 
{
    // Utility Function
    static int sumOfDivisblePowers(int L, int R, int P) 
    {
        // Traverse through all numbers from L to R
        int res = 0;
        for (int i = L; i <= R; i++)
        {
            // If P divides current number x, add 
            // largest power of P that divides x.
            int x = i;
            while (x % P == 0)
            {
                res++;
                x /= P;
            }
        }
          
        return res;
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        int L = 1, R = 10, P = 2;
        System.out.println(sumOfDivisblePowers(L, R, P));
    }
}
  
// This code is contributed by Anant Agarwal.




# Simple Python 3 Program to find sum of largest
# divisible powers of P in [L, R]
  
def sumOfDivisblePowers(L, R, P): 
  
    # Traverse through all numbers from L to R
    res = 0
    for i in range(L, R + 1):
      
        # If P divides current number x, add 
        # largest power of P that divides x.
        x = i
        while (x % P == 0):
          
            res += 1
            x /= P
      
    return res
  
  
# Driver code
L = 1
R = 10
P = 2
print(sumOfDivisblePowers(L, R, P))
  
# This code is contributed by Smitha Dinesh Semwal




// Simple C# Program to find sum of
// largest divisible powers of P in 
// [L, R]
using System;
  
class GFG 
{
    // Utility Function
    static int sumOfDivisblePowers(int L, int R, int P) 
    {
        // Traverse through all numbers 
        // from L to R
        int res = 0;
        for (int i = L; i <= R; i++)
        {
            // If P divides current number,
            // x, add largest power of P 
            // that divides x.
            int x = i;
            while (x % P == 0)
            {
                res++;
                x /= P;
            }
        }
          
        return res;
    }
  
    // Driver code
    public static void Main () 
    {
        int L = 1, R = 10, P = 2;
        Console.WriteLine(sumOfDivisblePowers(L, R, P));
    }
}
  
// This code is contributed by vt_m.




<?php
// Simple PHP Program to
// find sum of largest
// divisible powers of 
// P in [L, R]
  
// function return result
function sumOfDivisblePowers($L, $R, $P
{
  
    // Traverse through all 
    // numbers from L to R
    $res = 0;
    for ($i = $L; $i <= $R; $i++)
    {
          
        // If P divides current
        // number x, add 
        // largest power of 
        // P that divides x.
        $x = $i;
        while ($x % $P == 0)
        {
            $res++;
            $x /= $P;
        }
}
  
return $res;
}
  
    // Driver Code
    $L = 1; 
    $R = 10; 
    $P = 2;
    echo sumOfDivisblePowers($L, $R, $P);
  
// This code is contributed by ajit
?>




<script>
    // Simple Javascript Program to find sum of
    // largest divisible powers of P in 
    // [L, R]
      
    // Utility Function
    function sumOfDivisblePowers(L, R, P) 
    {
        // Traverse through all numbers 
        // from L to R
        let res = 0;
        for (let i = L; i <= R; i++)
        {
            // If P divides current number,
            // x, add largest power of P 
            // that divides x.
            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));
      
    // This code is contributed by divyeshrabadiya07.
</script>

Output: 

8

 

 
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) 
 

 




// Efficient CPP Program to find sum of 
// largest divisible powers of P in [L, R]
#include <bits/stdc++.h>
using namespace std;
  
// Implements Lagrange's theorem (Finds largest
// power of P that divides x!
int largestPower(int x, int P)
    // Calculate x/p + x/(p^2) + x/(p^3) + ....
    int res = 0;
    while (x)
    {
        x /= P;
        res += x;
    }
    return res;
}
  
// Returns sum of largest powers of P that divide
// numbers in range from L to R.
int sumOfDivisblePowers(int L, int R, int P) {
    return largestPower(R, P) - largestPower(L-1, P);
}
  
// Driver code
int main() {
  int L = 1, R = 10, P = 2;
  cout << sumOfDivisblePowers(L, R, P);
  return 0;
}




// Efficient CPP Program to find sum of 
// largest divisible powers of P in [L, R]
  
class GFG 
{
    // Implements Lagrange's theorem 
    // (Finds largest power of P that
    // divides x!
    static int largestPower(int x, int P)
    
        // Calculate x/p + x/(p^2) + x/(p^3) + ....
        int res = 0;
        while(x != 0)
        {
            x /= P;
            res += x;
        }
        return res;
    }
  
    // Returns sum of largest powers 
    // of P that divide numbers in 
    // range from L to R.
    static int sumOfDivisblePowers(int L, int R, int P) 
    {
        return largestPower(R, P) - largestPower(L-1, P);
    }
  
    // Driver code
    public static void main (String[] args)
    {
        int L = 1, R = 10, P = 2;
        System.out.println(sumOfDivisblePowers(L, R, P));
    }
}
  
// This code is contributed by Anant Agarwal.




# Efficient Python 3 Program to find sum of 
# largest divisible powers of P in [L, R]
  
# Implements Lagrange's theorem (Finds largest
# power of P that divides x!
def largestPower(x, P):
  
    # Calculate x/p + x/(p^2) + x/(p^3) + ....
    res = 0
    while (x):
      
        x = int(x / P)
        res += x
      
    return res
  
  
# Returns sum of largest powers of P that divide
# numbers in range from L to R.
def sumOfDivisblePowers( L, R, P) :
    return largestPower(R, P) - largestPower(L-1, P)
  
  
# Driver code
L = 1
R = 10
P = 2
print(sumOfDivisblePowers(L, R, P))
  
# This code is contributed by Smitha Dinesh Semwal




// Efficient C# Program to find sum of 
// largest divisible powers of P in [L, R]
using System;
  
class GFG {
      
    // Implements Lagrange's theorem (Finds
    // largest power of P that divides x!
    static int largestPower(int x, int P)
    
          
        // Calculate x/p + x/(p^2) + 
        // x/(p^3) + ....
        int res = 0;
        while (x>0)
        {
            x /= P;
            res += x;
        }
          
        return res;
    }
      
    // Returns sum of largest powers of
    // P that divide numbers in range 
    // from L to R.
    static int sumOfDivisblePowers(int L,
                            int R, int P)
    {
        return largestPower(R, P) 
                  - largestPower(L-1, P);
    }
      
    // Driver Program to test above
    // function
    static void Main() 
    {
        int L = 1, R = 10, P = 2;
        Console.Write(
            sumOfDivisblePowers(L, R, P));
    }
}
  
// This code is contributed by Anuj_67




<?php
// Efficient php Program to find sum of 
// largest divisible powers of P in [L, R]
  
// Implements Lagrange's theorem (Finds largest
// power of P that divides x!
function largestPower($x, $P)
  
    // Calculate x/p + x/(p^2) + x/(p^3) + ....
    $res = 0;
    while ($x)
    {
        $x = floor($x/$P);
        $res += $x;
    }
    return $res;
}
  
// Returns sum of largest powers of P that divide
// numbers in range from L to R.
function sumOfDivisblePowers($L, $R, $P) {
    return largestPower($R, $P) - largestPower($L-1, $P);
}
  
// Driver code
$L = 1;
$R = 10;
$P = 2;
echo sumOfDivisblePowers($L, $R, $P);
  
//This code is contributed by mits.
?>




<script>
    // Efficient Javascript Program to find sum of
    // largest divisible powers of P in [L, R]
      
    // Implements Lagrange's theorem (Finds
    // largest power of P that divides x!
    function largestPower(x, P)
    {
           
        // Calculate x/p + x/(p^2) +
        // x/(p^3) + ....
        let res = 0;
        while (x>0)
        {
            x = parseInt(x / P, 10);
            res += x;
        }
           
        return res;
    }
       
    // Returns sum of largest powers of
    // P that divide numbers in range
    // from L to R.
    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));
      
    // This code is contributed by mukesh07.
</script>

Output: 
8

 


Article Tags :