给定正整数N。任务是检查N是否为阿喀琉斯数。如果N是阿基里斯数,则打印“ YES”,否则打印“ NO”。
阿喀琉斯数:在数学中,阿喀琉斯数是一个强大的数(如果对于每个素数p,p 2也将其除,则数字n被认为是有幂数),但不是完美的幂。
前几个跟腱数是-
72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125, 1152, 1323
例子:
Input : 72
Output : YES
72 is powerful as 6 and 36 both divide it and it is not perfect square.
Input : 36
Output : NO
Explanation : 36 is powerful number but is perfect power.
先决条件:
- 强大的数字
方法
- 检查给定数字n是否为有效数字。要检查数字是否有效,请参考此内容。
- 检查n是否为完美幂。要了解检查数字是否为完美幂的各种方法,请参考此内容。
- 如果n强大但不完美,则n是阿喀琉斯数
否则不行。
以下是上述想法的实现。
C++
// Program to check if the given number is
// an Achilles Number
#include
using namespace std;
// function to check if the number
// is powerful number
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 number.
// Since prime numbers are not powerful, we
// return false if n is not 1.
return (n == 1);
}
// Utility function to check if
// number is a perfect power or not
bool isPower(int a)
{
if (a == 1)
return true;
for (int i = 2; i * i <= a; i++) {
double val = log(a) / log(i);
if ((val - (int)val) < 0.00000001)
return true;
}
return false;
}
// Function to check Achilles Number
bool isAchillesNumber(int n)
{
if (isPowerful(n) && !isPower(n))
return true;
else
return false;
}
// Driver Program
int main()
{
int n = 72;
if (isAchillesNumber(n))
cout << "YES" << endl;
else
cout << "NO" << endl;
n = 36;
if (isAchillesNumber(n))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Java
// Program to check if the
// Given number is
// an Achilles Number
class GFG {
// function to check if the number
// is powerful number
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 number.
// Since prime numbers are not powerful, we
// return false if n is not 1.
return (n == 1);
}
// Utility function to check if
// number is a perfect power or not
static boolean isPower(int a)
{
if (a == 1)
return true;
for (int i = 2; i * i <= a; i++) {
double val = Math.log(a) / Math.log(i);
if ((val - (int)val) < 0.00000001)
return true;
}
return false;
}
// Function to check Achilles Number
static boolean isAchillesNumber(int n)
{
if (isPowerful(n) && !isPower(n))
return true;
else
return false;
}
// Driver Program
public static void main(String[] args)
{
int n = 72;
if (isAchillesNumber(n))
System.out.println("YES");
else
System.out.println("NO");
n = 36;
if (isAchillesNumber(n))
System.out.println("YES");
else
System.out.println("NO");
}
}
Python3
# Program to check if the given number
# is an Achilles Number
from math import sqrt, log
# function to check if the number
# is powerful number
def isPowerful(n):
# First divide the number repeatedly by 2
while (n % 2 == 0):
power = 0
while (n % 2 == 0):
n /= 2
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
p = int(sqrt(n)) + 1
for factor in range(3, p, 2):
# Find highest power of "factor"
# that divides n
power = 0
while (n % factor == 0):
n = n / factor
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 number.
# Since prime numbers are not powerful, we
# return false if n is not 1.
return (n == 1)
# Utility function to check if
# number is a perfect power or not
def isPower(a):
if (a == 1):
return True
p = int(sqrt(a)) + 1
for i in range(2, a, 1):
val = log(a) / log(i)
if ((val - int(val)) < 0.00000001):
return True
return False
# Function to check Achilles Number
def isAchillesNumber(n):
if (isPowerful(n) == True and
isPower(n) == False):
return True
else:
return False
# Driver Code
if __name__ == '__main__':
n = 72
if (isAchillesNumber(n)):
print("YES")
else:
print("NO")
n = 36
if (isAchillesNumber(n)):
print("YES")
else:
print("NO")
# This code is contributed by
# Surendra_Gangwar
C#
// Program to check if the given number is
// an Achilles Number
using System;
class GFG {
// function to check if the number
// is powerful number
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 number.
// Since prime numbers are not powerful,
// we return false if n is not 1.
return (n == 1);
}
// Utility function to check if
// number is a perfect power or not
static bool isPower(int a)
{
if (a == 1)
return true;
for (int i = 2; i * i <= a; i++) {
double val = Math.Log(a) / Math.Log(i);
if ((val - (int)val) < 0.00000001)
return true;
}
return false;
}
// Function to check Achilles Number
static bool isAchillesNumber(int n)
{
if (isPowerful(n) && !isPower(n))
return true;
else
return false;
}
// Driver Program
public static void Main()
{
int n = 72;
if (isAchillesNumber(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
n = 36;
if (isAchillesNumber(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
PHP
输出:
YES
NO