📜  强大的数字

📅  最后修改于: 2021-04-24 14:46:03             🧑  作者: Mango

如果对于每个素数p,p 2也会除以n,则称n为有力数。例如:-36是一个强大的数字。它可以被3和3的平方整除,即9。
前几个有效数字是:
1,4,8,9,16,25,27,32,36,49,64…。
给定数字n,我们的任务是检查它是否有效。
例子 :

Input: 27
Output: YES

Input: 32
Output: YES

Input: 12
Output: NO

这个想法是基于这样一个事实,即如果数n是强大的,则该数及其平方的所有素数都应被n整除。我们找到给定数量的所有主要因素。对于每个素数,我们发现它除以n的最大幂。如果找到一个最大除数为1的素数,则返回false。如果所有主要因子的最高除法数均大于1,则返回true。
以下是上述想法的实现。

C++
// C++ program to find if a number is powerful or not.
#include 
using namespace std;
 
// function to check if the number is powerful
bool isPowerful(int n)
{
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        int power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
 
        // If only 2^1 divides n (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // if n is not a power of 2 then this loop will execute
    // repeat above process
    for (int factor = 3; factor <= sqrt(n); factor += 2) {
        // Find highest power of "factor" that divides n
        int power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
 
        // If only factor^1 divides n (not higher powers),
        // then return false
        if (power == 1)
            return false;
    }
 
    // n must be 1 now if it is not a prime numenr.
    // Since prime numbers are not powerful, we return
    // false if n is not 1.
    return (n == 1);
}
 
// Driver program to test above function
int main()
{
    isPowerful(20) ? cout << "YES\n" : cout << "NO\n";
    isPowerful(27) ? cout << "YES\n" : cout << "NO\n";
 
    return 0;
}


Java
// Java program to find if a
// number is powerful or not.
 
class GFG {
    // function to check if the
    // number is powerful
    static boolean isPowerful(int n)
    {
        // First divide the number
        // repeatedly by 2
        while (n % 2 == 0) {
            int power = 0;
            while (n % 2 == 0) {
                n /= 2;
                power++;
            }
 
            // If only 2^1 divides n (not higher powers),
            // then return false
            if (power == 1)
                return false;
        }
 
        // if n is not a power of 2 then this loop will execute
        // repeat above process
        for (int factor = 3; factor <= Math.sqrt(n); factor += 2) {
            // Find highest power of "factor" that divides n
            int power = 0;
            while (n % factor == 0) {
                n = n / factor;
                power++;
            }
 
            // If only factor^1 divides n (not higher powers),
            // then return false
            if (power == 1)
                return false;
        }
 
        // n must be 1 now if it is not a prime numenr.
        // Since prime numbers are not powerful, we return
        // false if n is not 1.
        return (n == 1);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        if (isPowerful(20))
            System.out.print("YES\n");
        else
            System.out.print("NO\n");
        if (isPowerful(27))
            System.out.print("YES\n");
        else
            System.out.print("NO\n");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# if a number is powerful or not.
import math
 
# function to check if
# the number is powerful
def isPowerful(n):
 
    # First divide the number repeatedly by 2
    while (n % 2 == 0):
 
        power = 0
        while (n % 2 == 0):
         
            n = n//2
            power = power + 1
         
          
        # If only 2 ^ 1 divides
        # n (not higher powers),
        # then return false
        if (power == 1):
            return False
     
  
    # if n is not a power of 2
    # then this loop will execute
    # repeat above process
    for factor in range(3, int(math.sqrt(n))+1, 2):
     
        # Find highest power of
        # "factor" that divides n
        power = 0
        while (n % factor == 0):
         
            n = n//factor
            power = power + 1
         
  
        # If only factor ^ 1 divides
        # n (not higher powers),
        # then return false
        if (power == 1):
            return false
     
  
     # n must be 1 now if it
     # is not a prime numenr.
     # Since prime numbers are
     # not powerful, we return
     # false if n is not 1.
    return (n == 1)
 
# Driver code
 
print("YES" if isPowerful(20) else "NO")
print("YES" if isPowerful(27) else "NO")
 
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find if a
// number is powerful or not.
using System;
 
class GFG {
 
    // function to check if the
    // number is powerful
    static bool isPowerful(int n)
    {
        // First divide the number
        // repeatedly by 2
        while (n % 2 == 0) {
            int power = 0;
            while (n % 2 == 0) {
                n /= 2;
                power++;
            }
 
            // If only 2^1 divides n
            // (not higher powers),
            // then return false
            if (power == 1)
                return false;
        }
 
        // if n is not a power of 2 then
        // this loop will execute repeat
        // above process
        for (int factor = 3; factor <= Math.Sqrt(n); factor += 2) {
            // Find highest power of "factor"
            // that divides n
            int power = 0;
            while (n % factor == 0) {
                n = n / factor;
                power++;
            }
 
            // If only factor^1 divides n
            // (not higher powers), then
            // return false
            if (power == 1)
                return false;
        }
 
        // n must be 1 now if it is not
        // a prime numenr.
        // Since prime numbers are not
        // powerful, we return false if
        // n is not 1.
        return (n == 1);
    }
 
    // Driver code
    public static void Main()
    {
        if (isPowerful(20))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
 
        if (isPowerful(27))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

NO
YES