Check if a number is a Trojan Number Last Updated : 11 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a Number N . 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 mk, 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 xy. 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; } Java // Java program to check if a number is // Trojan Number or not import java.util.*; class GFG { // Function to check if a number // can be expressed as x^y static boolean isPerfectPower(int n) { if (n == 1) { return true; } // Try all numbers from 2 to sqrt(n) as base for (int x = 2; x <= Math.sqrt(n); x++) { int y = 2; int p = (int) Math.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 = (int) Math.pow(x, y); } } return false; } // Function to check if a number is Strong static boolean isStrongNumber(int n) { HashMap<Integer, Integer> count = new HashMap<Integer, Integer>(); while (n % 2 == 0) { n = n / 2; if (count.containsKey(2)) { count.put(2, count.get(2) + 1); } else { count.put(2, 1); } } // count the number for each prime factor for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { n = n / i; if (count.containsKey(i)) { count.put(i, count.get(i) + 1); } else { count.put(i, 1); } } } if (n > 2) { if (count.containsKey(n)) { count.put(n, count.get(n) + 1); } else { count.put(n, 1); } } int flag = 0; for (Map.Entry<Integer, Integer> b : count.entrySet()) { // minimum number of prime divisors // should be 2 if (b.getValue() == 1) { flag = 1; break; } } if (flag == 1) { return false; } else { return true; } } // Function to check if a number // is Trojan Number static boolean isTrojan(int n) { if (!isPerfectPower(n) && isStrongNumber(n)) { return true; } else { return false; } } // Driver Code public static void main(String[] args) { int n = 108; if (isTrojan(n)) { System.out.println("Yes"); } else { System.out.println("No"); } } } // This code is contributed by PrinciRaj1992 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 = 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 = n // i count[i] += 1 if n > 2: count[n] += 1 flag = 0 for key,value in count.items(): # minimum number of prime # divisors should be 2 if value == 1: flag = 1 break if flag == 1: return False return True # Function to check if a number # is Trojan Number def isTrojan(n): return isPerfectPower(n) == False and isStrongNumber(n) # Driver Code if __name__ == '__main__': n = 108 if (isTrojan(n)): print("YES") else: print("NO") # This code is contributed by # Surendra_Gangwar C# // C# program to check if a number is // Trojan Number or not using System; using System.Collections.Generic; class GFG { // Function to check if a number // can be expressed as x^y static bool isPerfectPower(int n) { if (n == 1) { return true; } // Try all numbers from 2 to sqrt(n) as base for (int x = 2; x <= Math.Sqrt(n); x++) { int y = 2; int p = (int) Math.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 = (int) Math.Pow(x, y); } } return false; } // Function to check if a number is Strong static bool isStrongNumber(int n) { Dictionary<int, int> count = new Dictionary<int, int>(); while (n % 2 == 0) { n = n / 2; if (count.ContainsKey(2)) { count[2] = count[2] + 1; } else { count.Add(2, 1); } } // count the number for each prime factor for (int i = 3; i <= Math.Sqrt(n); i += 2) { while (n % i == 0) { n = n / i; if (count.ContainsKey(i)) { count[i] = count[i] + 1; } else { count.Add(i, 1); } } } if (n > 2) { if (count.ContainsKey(n)) { count[n] = count[n] + 1; } else { count.Add(n, 1); } } int flag = 0; foreach(KeyValuePair<int, int> b in count) { // minimum number of prime divisors // should be 2 if (b.Value == 1) { flag = 1; break; } } if (flag == 1) { return false; } else { return true; } } // Function to check if a number // is Trojan Number static bool isTrojan(int n) { if (!isPerfectPower(n) && isStrongNumber(n)) { return true; } else { return false; } } // Driver Code public static void Main(String[] args) { int n = 108; if (isTrojan(n)) { Console.WriteLine("Yes"); } else { Console.WriteLine("No"); } } } // This code is contributed by Princi Singh JavaScript <script> // javascript program to check if a number is // Trojan Number or not // Function to check if a number // can be expressed as x^y function isPerfectPower(n) { if (n == 1) { return true; } // Try all numbers from 2 to sqrt(n) as base for (var x = 2; x <= Math.sqrt(n); x++) { var y = 2; var p = parseInt( Math.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 = parseInt( Math.pow(x, y)); } } return false; } // Function to check if a number is Strong function isStrongNumber(n) { var count = new Map(); while (n % 2 == 0) { n = n / 2; if (count.has(2)) { count.set(2, count.get(2) + 1); } else { count.set(2, 1); } } // count the number for each prime factor for (var i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { n = n / i; if (count.has(i)) { count.set(i, count.get(i) + 1); } else { count.set(i, 1); } } } if (n > 2) { if (count.has(n)) { count.set(n, count.get(n) + 1); } else { count.set(n, 1); } } var flag = 0; const iterator = count[Symbol.iterator](); let itr = iterator.next() for (let i = 0; i < count.size; i++) { console.log(itr.value, itr.done) if (itr.value == 1) { flag = 1; break; } itr = iterator.next() } if (flag == 1) { return false; } else { return true; } } // Function to check if a number // is Trojan Number function isTrojan(n) { if (!isPerfectPower(n) && isStrongNumber(n)) { return true; } else { return false; } } // Driver Code var n = 108; if (isTrojan(n)) { document.write("Yes"); } else { document.write("No"); } // This code contributed by gauravrajput1 </script> Output: YES Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Check if a number is a Trojan Number K Kushdeep_Mittal Follow Improve Article Tags : Mathematical DSA Technical Scripter 2018 Practice Tags : Mathematical Similar Reads Check if a number is Triperfect Number Given a number N . The tasks is to check if the given number is a Triperfect Number.Triperfect Number: A Number is a Triperfect Number if it is equal to three times the sum of its divisors, that is, 3 times sum of its positive divisors.Examples: Input: N = 15 Output: false Divisors of 15 are 1, 3, 5 7 min read Check if a number is a Mystery Number Given a number, check whether it is a mystery number or not. A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. Examples: Input : n = 121 Output : 29 92 Input : n = 22 Output : 11 11 Source: Paytm Interview Set 23Th 4 min read PHP | Check if a number is armstrong number Given a number, we need to check whether it is an armstrong number or not in PHP. An Armstrong number is the one whose value is equal to the sum of the cubes of its digits.Examples: Input : 407 Output : Yes 407 = (4*4*4) + (0*0*0) + (7*7*7) = 64 + 0 + 343 = 407 Input : 303 Output : No Approach: For 2 min read Check if a number is jumbled or not Write a program to check if a given integer is jumbled or not. A number is said to be Jumbled if for every digit, its neighbours digit differs by max 1. Examples : Input : 6765Output : TrueAll neighbour digits differ by atmost 1. Input : 1223Output : True Input : 1235Output : False Approach: Find th 6 min read Check if a number is a Krishnamurthy Number or not A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.For example, 145 is the sum of the factorial of each digit.1! + 4! + 5! = 1 + 24 + 120 = 145 Examples: Input : 145Output : YESExplanation: 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to input, 10 min read Check Whether a number is Duck Number or not A Duck number is a positive number which has zeroes present in it, For example 3210, 8050896, 70709 are all Duck numbers. Please note that a numbers with only leading 0s is not considered as Duck Number. For example, numbers like 035 or 0012 are not considered as Duck Numbers. A number like 01203 is 8 min read Check whether a given number N is a Nude Number or not Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output 4 min read Check if a number is Full Prime A full prime number is one in which the number itself is prime and all its digits are also prime. Given a number n, check if it is Full Prime or not.Examples : Input : 53 Output : Yes Explanation: Number 53 is prime and its digits are also prime. Input : 41 Output : No Explanation: Number 41 is prim 7 min read Check whether a number is Tech Number or not Given a number, the task is to check if it is a Tech number or not. Note: A Tech number is a number that has an even number of digits and if the number is split into two equal halves(From the middle), then the square of the sum of these halves is equal to the number itself. Examples:Input: n = 81 Ou 6 min read Program to check for a Valid IMEI Number International Mobile Equipment Identity (IMEI) is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone, but can also be displayed on-screen on most phones by entering *#06# on the dialpad, or al 6 min read Like