Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a number is an Achilles number or not in Python
An Achilles number is a number that is powerful but not a perfect power. A powerful number means that for every prime factor p, p² also divides the number. A perfect power is a number that can be expressed as a^b where a and b are positive integers and b > 1.
Some examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.
Understanding Achilles Numbers
For example, 108 is an Achilles number because:
- 108 = 2² × 3³ (powerful: both 2² and 3² divide 108)
- 108 cannot be expressed as a^b for integers a, b > 1
Algorithm Steps
To check if a number is an Achilles number ?
- Check if the number is powerful
- Check if the number is NOT a perfect power
- Return True only if both conditions are met
Implementation
Function to Check if Number is Powerful
from math import sqrt, log
def check_powerful(n):
# Check for factor 2
while (n % 2 == 0):
p = 0
while (n % 2 == 0):
n //= 2
p += 1
if (p == 1):
return False
# Check for odd factors
limit = int(sqrt(n)) + 1
for factor in range(3, limit, 2):
p = 0
while (n % factor == 0):
n = n // factor
p += 1
if (p == 1):
return False
return (n == 1)
# Test the function
print(check_powerful(108))
print(check_powerful(12))
True False
Function to Check if Number is Perfect Power
def check_power(a):
if (a == 1):
return True
# Check for possible bases up to sqrt(a)
limit = int(sqrt(a)) + 1
for i in range(2, limit):
val = log(a) / log(i)
if (abs(val - round(val)) < 1e-9):
return True
return False
# Test the function
print(check_power(8)) # 2^3
print(check_power(108)) # Not a perfect power
True False
Complete Achilles Number Checker
from math import sqrt, log
def check_powerful(n):
# Check for factor 2
while (n % 2 == 0):
p = 0
while (n % 2 == 0):
n //= 2
p += 1
if (p == 1):
return False
# Check for odd factors
limit = int(sqrt(n)) + 1
for factor in range(3, limit, 2):
p = 0
while (n % factor == 0):
n = n // factor
p += 1
if (p == 1):
return False
return (n == 1)
def check_power(a):
if (a == 1):
return True
limit = int(sqrt(a)) + 1
for i in range(2, limit):
val = log(a) / log(i)
if (abs(val - round(val)) < 1e-9):
return True
return False
def isAchilles(n):
return check_powerful(n) and not check_power(n)
# Test with multiple numbers
test_numbers = [72, 108, 200, 64, 100]
for num in test_numbers:
result = isAchilles(num)
print(f"{num} is {'an Achilles' if result else 'not an Achilles'} number")
72 is an Achilles number 108 is an Achilles number 200 is an Achilles number 64 is not an Achilles number 100 is not an Achilles number
Example Analysis
Let's verify why 108 is an Achilles number ?
n = 108
print(f"Checking {n}:")
print(f"Is powerful: {check_powerful(n)}")
print(f"Is perfect power: {check_power(n)}")
print(f"Is Achilles: {isAchilles(n)}")
# Prime factorization: 108 = 2² × 3³
print(f"\nPrime factorization of {n}: 2² × 3³")
print("Powerful because 2² and 3² both divide 108")
print("Not a perfect power because it cannot be expressed as a^b")
Checking 108: Is powerful: True Is perfect power: False Is Achilles: True Prime factorization of 108: 2² × 3³ Powerful because 2² and 3² both divide 108 Not a perfect power because it cannot be expressed as a^b
Conclusion
An Achilles number must be both powerful (every prime factor appears with power ? 2) and not a perfect power. The algorithm checks both conditions by examining prime factorization and testing for perfect power relationships using logarithms.
