📌  相关文章
📜  查找给定数字的超能力

📅  最后修改于: 2021-04-29 04:28:02             🧑  作者: Mango

给定一个整数n 。任务是从…的因式分解中找到超能力n

在数n的因式分解中,超级幂是质数幂中的最高幂。

例子

Input :  n = 32
Output :  5

Input : n = 240
Output : 4

用于查找任何给定数字的超能力n ,我们必须完成n的分解,并找出所有素数中的最高幂。

注意:使用Sieve来存储素数列表对于优化很有用。

算法

  • 迭代素数并计算n的因式分解。
  • 对于存储的素数列表中的每个素数(也是n的因数),
    找到它的力量并检查它的超级力量。

下面是上述方法的实现:

C++
// CPP for finding super power of n
#include 
#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;
}


Java
// 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


Python3
# 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


C#
// 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


PHP
 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
?>


输出:
8