Open In App

Check if a number is an Achilles number or not

Improve
Improve
Like Article
Like
Save
Share
Report

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 
Explanation :  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++




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


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




<?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
?>


Javascript




<script>
 
// Javascript 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)
    {
        let power = 0;
         
        while (n % 2 == 0)
        {
            n = parseInt(n / 2, 10);
            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(let factor = 3;
            factor <= Math.sqrt(n);
            factor += 2)
    {
 
        // Find highest power of "factor" that
        //  divides n
        let power = 0;
         
        while (n % factor == 0)
        {
            n = parseInt(n / factor, 10);
            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(let i = 2; i * i <= a; i++)
    {
        let val = Math.log(a) / Math.log(i);
         
        if ((val - parseInt(val, 10)) < 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
let n = 72;
if (isAchillesNumber(n))
    document.write("YES" + "</br>");
else
    document.write("NO" + "</br>");
 
n = 36;
if (isAchillesNumber(n))
    document.write("YES" + "</br>");
else
    document.write("NO" + "</br>");
     
// This code is contributed by suresh07
 
</script>


Output: 

YES
NO

 

Time complexity: O(sqrt(n))
Auxiliary space: O(1)



Last Updated : 16 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads