Check if a number is an Achilles number or not in C++

C++Server Side ProgrammingProgramming

Concept

With respect of given positive integer n, the task is to verify if n is an Achilles number or not. We have to print 'YES' if N is treated as an Achilles number else print 'NO'.

Achilles number: With respect of Mathematics, an Achilles number is defined as a number that is powerful (A number N is said to be Powerful Number if it has been noted that for every prime factor p of it, p^2 also divides it) but not a perfect power.

In following, the first few Achilles number are displayed72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125

Input − 108

Output − YES

108 is powerful as 6 and 36 both divide it and it is not perfect square.

Input − 64

Output − NO

Explanation − 64 is powerful number but is perfect power.

Approach

  • Verify if the given number N is a powerful number or not.

  • Verify if N is a perfect power or not.

  • If N is powerful but not perfect then, N is an Achilles Number.Otherwise it is not.

Example

 Live Demo

// CPP program to check Primorial Prime
#include <bits/stdc++.h>
using namespace std;
bool isPowerful1(int n1){
   while (n1 % 2 == 0) {
      int power1 = 0;
      while (n1 % 2 == 0) {
         n1 /= 2;
         power1++;
      }
      if (power1 == 1)
         return false;
      }
      for (int factor1 = 3; factor1 <= sqrt(n1); factor1 += 2) {
         int power1 = 0;
         while (n1 % factor1 == 0) {
            n1 = n1 / factor1;
            power1++;
         }
         if (power1 == 1)
            return false;
         }
         return (n1 == 1);
      }
      bool isPower1(int a1){
         if (a1 == 1)
            return true;
         for (int i1 = 2; i1 * i1 <= a1; i1++) {
            double val1 = log(a1) / log(i1);
            if ((val1 - (int)val1) < 0.00000001)
               return true;
            }
            return false;
         }
         bool isAchillesNumber1(int n1){
            if (isPowerful1(n1) && !isPower1(n1))
               return true;
            else
               return false;
         }
// Driver Program
int main(){
   int n1 = 108;
   if (isAchillesNumber1(n1))
      cout << "YES" << endl;
   else
      cout << "NO" << endl;
   n1 = 35;
   if (isAchillesNumber1(n1))
      cout << "YES" << endl;
   else
      cout << "NO" << endl;
   return 0;
}

Output

YES
NO
raja
Published on 23-Jul-2020 08:03:34
Advertisements