Check if a number is an Achilles number or not

Given a positive integer N. The task is to check if N is an Achilles number or not. Print ‘YES’ if N is an Achilles number else print ‘NO’.

Achilles number: In Mathematics, an Achilles number is a number that is powerful ( A number n is said to be Powerful Number if for every prime factor p of it, p2 also divides it ) but not a perfect power.

The first few Achilles number are-

72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125, 1152, 1323

Examples:

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.

Prerequisite:

Approach

  1. Check If the given number n is a powerful number or not. To check if a number is powerful or not refer this.
  2. Check if n is a perfect power or not. To know various approaches to check if a number is perfect power or not – refer this.
  3. If n is powerful but not perfect then, n is an Achilles Number
    Otherwise Not.

Below is the implementation of above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check if the given number is
// an Achilles Number
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


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 [tabby title="C#"]

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to check if the given number 
// is an Achilles Number 
  
// Function to check if the number 
// is powerful number 
function isPowerful($n
    // First divide the number 
    // repeatedly by 2 
    while ($n % 2 == 0)
    
        $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 ($factor = 3; $factor <= sqrt($n);
                      $factor += 2) 
    
  
        // Find highest power of "factor" 
        // that divides n 
        $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 
function isPower($a
    if ($a == 1) 
        return true; 
  
    for ($i = 2; $i * $i <= $a; $i++)
    
        $val = log($a) / log($i); 
        if (($val - (int)$val) < 0.00000001) 
            return true; 
    
  
    return false; 
  
// Function to check Achilles Number 
function isAchillesNumber($n
    if (isPowerful($n) && !isPower($n)) 
        return true; 
    else
        return false; 
  
// Driver Code
$n = 72; 
if (isAchillesNumber($n)) 
    echo "YES", "\n"
else
    echo "NO", "\n"
  
$n = 36; 
if (isAchillesNumber($n)) 
echo "YES","\n"
else
    echo "NO" ,"\n"
  
// This code is contributed by ajit
?>

chevron_right


Output:

YES
NO


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, SURENDRA_GANGWAR