Find the super power of a given Number

Given an integer n. The task is to find the superpower from the factorization of n.

The Superpower is the highest power among the power of primes in the factorisation of a number n.

Examples:

Input :  n = 32
Output :  5

Input : n = 240
Output : 4

For finding the superpower of any given number n, we have to complete the factorisation of n, and find out highest power among all of the prime factors.

Note: Using Sieve for the purpose of storing list of primes is useful in terms of optimization.

Algorithm :

  • Itereate over primes and calculate the factorization of n.
  • For each prime among the stored list of primes and which is also a factor of n,
    find its power and check it for super power.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP for finding super power of n
#include <bits/stdc++.h>
#define MAX 100000
using namespace std;
  
// global hash for prime
bool prime[100002];
  
// sieve method for storing a list of prime
void SieveOfEratosthenes()
{
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p <= MAX; p++)
        if (prime[p] == true)
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
}
  
// function to return super power
int superpower(int n)
{
    SieveOfEratosthenes();
    int superPower = 0, factor = 0;
    int i = 2;
    // find the super power
    while (n > 1 && i <= MAX) {
        if (prime[i]) {
            factor = 0;
            while (n % i == 0 && n > 1) {
                factor++;
                n = n / i;
            }
  
            if (superPower < factor)
                superPower = factor;
        }
        i++;
    }
  
    return superPower;
}
  
// Driver program
int main()
{
    int n = 256;
    cout << superpower(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java for finding super power of n 
  
class GFG{ 
static int MAX=100000
// global hash for prime 
static boolean[] prime=new boolean[100002]; 
  
// sieve method for storing a list of prime 
static void SieveOfEratosthenes() 
  
    for (int p = 2; p * p <= MAX; p++) 
        if (prime[p] == false
            for (int i = p * 2; i <= MAX; i += p) 
                prime[i] = true
  
// function to return super power 
static int superpower(int n) 
    SieveOfEratosthenes(); 
    int superPower = 0, factor = 0
    int i = 2
    // find the super power 
    while (n > 1 && i <= MAX) { 
        if (!prime[i]) { 
            factor = 0
            while (n % i == 0 && n > 1) { 
                factor++; 
                n = n / i; 
            
  
            if (superPower < factor) 
                superPower = factor; 
        
        i++; 
    
  
    return superPower; 
  
// Driver program 
public static void main(String[] args) 
    int n = 256
    System.out.println(superpower(n));
}
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 for finding super
# power of n
MAX = 100000;
  
# global hash for prime
prime = [True] * 100002;
  
# sieve method for storing
# a list of prime
def SieveOfEratosthenes():
  
    p = 2;
    while(p * p <= MAX):
        if (prime[p] == True):
            i = p * 2;
            while(i <= MAX):
                prime[i] = False;
                i += p;
        p += 1;
  
# function to return super power
def superpower(n):
  
    SieveOfEratosthenes();
    superPower = 0;
    factor = 0;
    i = 2;
      
    # find the super power
    while (n > 1 and i <= MAX):
        if (prime[i]):
            factor = 0;
            while (n % i == 0 and n > 1):
                factor += 1;
                n = int(n / i);
  
            if (superPower < factor):
                superPower = factor;
        i += 1;
  
    return superPower;
  
# Driver Code
n = 256;
print(superpower(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# for finding super power of n 
  
class GFG
static int MAX = 100000; 
  
// global hash for prime 
static bool[] prime = new bool[100002]; 
  
// sieve method for storing
// a list of prime 
static void SieveOfEratosthenes() 
  
    for (int p = 2; 
             p * p <= MAX; p++) 
        if (prime[p] == false
            for (int i = p * 2; 
                     i <= MAX; i += p) 
                prime[i] = true
  
// function to return super power 
static int superpower(int n) 
    SieveOfEratosthenes(); 
    int superPower = 0, factor = 0; 
    int i = 2; 
      
    // find the super power 
    while (n > 1 && i <= MAX)
    
        if (!prime[i]) 
        
            factor = 0; 
            while (n % i == 0 && n > 1)
            
                factor++; 
                n = n / i; 
            
  
            if (superPower < factor) 
                superPower = factor; 
        
        i++; 
    
  
    return superPower; 
  
// Driver Code 
static void Main() 
    int n = 256; 
    System.Console.WriteLine(superpower(n));
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP for finding super power of n
$MAX = 100000;
  
// global hash for prime
$prime = array_fill(0, 100002, true);
  
// sieve method for storing
// a list of prime
function SieveOfEratosthenes()
{
    global $MAX, $prime;
  
    for ($p = 2; $p * $p <= $MAX; $p++)
        if ($prime[$p] == true)
            for ($i = $p * 2; 
                 $i <= $MAX; $i += $p)
                $prime[$i] = false;
}
  
// function to return super power
function superpower($n)
{
    SieveOfEratosthenes();
    global $MAX, $prime;
    $superPower = 0;
    $factor = 0;
    $i = 2;
    // find the super power
    while ($n > 1 && $i <= $MAX
    {
        if ($prime[$i])
        {
            $factor = 0;
            while ($n % $i == 0 && $n > 1) 
            {
                $factor++;
                $n = $n / $i;
            }
  
            if ($superPower < $factor)
                $superPower = $factor;
        }
        $i++;
    }
  
    return $superPower;
}
  
// Driver Code
$n = 256;
echo superpower($n);
  
// This code is contributed by mits
?>

chevron_right


Output:

8


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.



Improved By : Mithun Kumar