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.

CPP

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

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

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.