Check if a number is an Achilles number or not in Python


Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.

So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is not perfect square.

To solve this, we will follow these steps −

  • Define a function check_powerful() . This will take n
  • while n mod 2 is same, do
    • p := 0
    • while n mod 2 is same as 0, do
      • n := n / 2
      • p := p + 1
    • if p is same as 1, then
      • return False
  • p := integer of (square root of n) + 1
  • for factor in range 3 to p, increase by 2, do
    • p := 0
    • while n mod factor is same as 0, do
      • n := n / factor
      • p := p + 1
    • if p is same as 1, then
      • return False
  • return true when (n is same as 1)
  • Define a function check_power() . This will take a
  • if a is same as 1, then
    • return True
  • p := integer of (square root of n) + 1
  • for i in range 2 to a, increase by 1, do
    • val := log(a) / log(i) [all base e]
    • if (val - integer part of (val)) < 0.00000001, then
      • return True
  • return False
  • From the main method, do the following −
  • if check_powerful(n) is same as True and check_power(n) is same as False, then
    • return True
  • otherwise,
    • return False

Example

Let us see the following implementation to get better understanding −

 Live Demo

from math import sqrt, log
def check_powerful(n):
   while (n % 2 == 0):
      p = 0
      while (n % 2 == 0):
         n /= 2
         p += 1
      if (p == 1):
         return False  
   p = int(sqrt(n)) + 1
   for factor in range(3, p, 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
   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
def isAchilles(n):
   if (check_powerful(n) == True and check_power(n) == False):
      return True
   else:
      return False
n = 108
print(isAchilles(n))

Input

108

Output

True

Updated on: 27-Aug-2020

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements