# Check if a number is a Trojan Number

Given a Number . The task is to check if N is a Trojan Number or not.

**Trojan Number** is a number that is a strong number but not a perfect power. A number N is known as a strong number if, for every prime divisor or factor p of N, p2 is also a divisor. In other words, every prime factor appears at least twice.

All Trojan numbers are strong. However, not all strong numbers are Trojan numbers: only those that cannot be represented as m^{k}, where m and k are positive integers greater than 1.

**Examples**:

Input : N = 108 Output : YES Input : N = 8 Output : NO

The idea is to store the count of each prime factor and check if the count is greater than 2 then it will be a **Strong Number**.

This part can easily be calculated by prime factorization through sieve.

The next step is to check if the given number cannot be expressed as x^{y}. To check whether a number is **perfect power** or not refer to this article.

Below is the implementation of above problem:

## C++

`// CPP program to check if a number is ` `// Trojan Number or not ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if a number ` `// can be expressed as x^y ` `bool` `isPerfectPower(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `true` `; ` ` ` ` ` `// Try all numbers from 2 to sqrt(n) as base ` ` ` `for` `(` `int` `x = 2; x <= ` `sqrt` `(n); x++) { ` ` ` `int` `y = 2; ` ` ` `int` `p = ` `pow` `(x, y); ` ` ` ` ` `// Keep increasing y while power 'p' ` ` ` `// is smaller than n. ` ` ` `while` `(p <= n && p > 0) { ` ` ` `if` `(p == n) ` ` ` `return` `true` `; ` ` ` `y++; ` ` ` `p = ` `pow` `(x, y); ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Function to check if a number is Strong ` `bool` `isStrongNumber(` `int` `n) ` `{ ` ` ` `unordered_map<` `int` `, ` `int` `> count; ` ` ` `while` `(n % 2 == 0) { ` ` ` `n = n / 2; ` ` ` `count[2]++; ` ` ` `} ` ` ` ` ` `// count the number for each prime factor ` ` ` `for` `(` `int` `i = 3; i <= ` `sqrt` `(n); i += 2) { ` ` ` `while` `(n % i == 0) { ` ` ` `n = n / i; ` ` ` `count[i]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(n > 2) ` ` ` `count[n]++; ` ` ` ` ` `int` `flag = 0; ` ` ` ` ` `for` `(` `auto` `b : count) { ` ` ` ` ` `// minimum number of prime divisors ` ` ` `// should be 2 ` ` ` `if` `(b.second == 1) { ` ` ` `flag = 1; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(flag == 1) ` ` ` `return` `false` `; ` ` ` `else` ` ` `return` `true` `; ` `} ` ` ` `// Function to check if a number ` `// is Trojan Number ` `bool` `isTrojan(` `int` `n) ` `{ ` ` ` `if` `(!isPerfectPower(n) && isStrongNumber(n)) ` ` ` `return` `true` `; ` ` ` `else` ` ` `return` `false` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 108; ` ` ` ` ` `if` `(isTrojan(n)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python 3 program to check if a number

# is Trojan Number or not

from math import sqrt, pow

# Function to check if a number

# can be expressed as x^y

def isPerfectPower(n):

if (n == 1):

return True

# Try all numbers from 2 to

# sqrt(n) as base

for x in range(2, int(sqrt(n)) + 1):

y = 2

p = pow(x, y)

# Keep increasing y while power

# ‘p’ is smaller than n.

while (p <= n and p > 0):

if (p == n):

return True

y += 1

p = pow(x, y)

return False

# Function to check if a number

# is Strong

def isStrongNumber(n):

count = {i:0 for i in range(n)}

while (n % 2 == 0):

n = int(n / 2)

count[2] += 1

# count the number for each

# prime factor

for i in range(3,int(sqrt(n)) + 1, 2):

while (n % i == 0):

n = int(n / i)

count[i] += 1

if (n > 2):

count[n] += 1

flag = 0

for ket,value in count.items():

# minimum number of prime

# divisors should be 2

if (value == 1):

flag = 1

break

if (flag == 1):

return False

else:

return True

# Function to check if a number

# is Trojan Number

def isTrojan(n):

if (isPerfectPower(n) == False and

isStrongNumber(n)):

return True

else:

return False

# Driver Code

if __name__ == ‘__main__’:

n = 108

if (isTrojan(n)):

print(“YES”)

else:

print(“NO”)

# This code is contributed by

# Surendra_Gangwar

**Output:**

YES

## Recommended Posts:

- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check if a number is divisible by all prime divisors of another number
- Program to check whether a number is Proth number or not
- Check if a number with even number of digits is palindrome or not
- Check if a number is a power of another number
- Check Whether a number is Duck Number or not
- Check if given number is Emirp Number or not
- Check if a number is an Achilles number or not
- Check if a number is a Krishnamurthy Number or not
- Check if a number is a Mystery Number
- Check whether a number is Non-hypotenuse number
- Check if a number is Triperfect Number
- Check whether the given number is Euclid Number or not
- Check if a number is an Unusual Number or not

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.