Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a number is a Trojan Numbers in Python
A Trojan Number is a special type of number that meets two criteria: it must be a strong number but not a perfect power. A strong number is one where every prime factor appears at least twice (squared or higher). For example, 72 = 2³ × 3² is strong because both prime factors (2 and 3) appear at least twice, and it's not a perfect power like 8 = 2³.
Understanding Strong Numbers
A number is strong when every prime factor appears with an exponent of 2 or more ?
from math import sqrt
def check_strong_num(n):
# Count frequency of each prime factor
count = {}
original_n = n
# Handle factor 2
while n % 2 == 0:
n = n // 2
count[2] = count.get(2, 0) + 1
# Handle odd factors
for i in range(3, int(sqrt(n)) + 1, 2):
while n % i == 0:
n = n // i
count[i] = count.get(i, 0) + 1
# Handle remaining prime factor
if n > 2:
count[n] = count.get(n, 0) + 1
# Check if all prime factors appear at least twice
for value in count.values():
if value == 1:
return False
return True
# Test examples
print(f"72 is strong: {check_strong_num(72)}") # 72 = 2³ × 3²
print(f"12 is strong: {check_strong_num(12)}") # 12 = 2² × 3¹ (not strong)
72 is strong: True 12 is strong: False
Checking Perfect Powers
A perfect power is a number that can be expressed as a^b where a and b are positive integers and b ? 2 ?
from math import sqrt
def check_perfect_pow(n):
if n == 1:
return True
for base in range(2, int(sqrt(n)) + 1):
exponent = 2
power = base ** exponent
while power <= n and power > 0:
if power == n:
return True
exponent += 1
power = base ** exponent
return False
# Test examples
print(f"8 is perfect power: {check_perfect_pow(8)}") # 8 = 2³
print(f"72 is perfect power: {check_perfect_pow(72)}") # 72 is not a^b
8 is perfect power: True 72 is perfect power: False
Complete Trojan Number Check
A Trojan number must be strong but not a perfect power ?
from math import sqrt
def check_perfect_pow(n):
if n == 1:
return True
for base in range(2, int(sqrt(n)) + 1):
exponent = 2
power = base ** exponent
while power <= n and power > 0:
if power == n:
return True
exponent += 1
power = base ** exponent
return False
def check_strong_num(n):
count = {}
# Handle factor 2
while n % 2 == 0:
n = n // 2
count[2] = count.get(2, 0) + 1
# Handle odd factors
for i in range(3, int(sqrt(n)) + 1, 2):
while n % i == 0:
n = n // i
count[i] = count.get(i, 0) + 1
# Handle remaining prime factor
if n > 2:
count[n] = count.get(n, 0) + 1
# Check if all prime factors appear at least twice
for value in count.values():
if value == 1:
return False
return True
def isTrojan(n):
return not check_perfect_pow(n) and check_strong_num(n)
# Test cases
test_numbers = [72, 108, 200, 8, 12]
for num in test_numbers:
result = isTrojan(num)
print(f"{num} is Trojan: {result}")
72 is Trojan: True 108 is Trojan: True 200 is Trojan: True 8 is Trojan: False 12 is Trojan: False
Examples Breakdown
| Number | Prime Factorization | Strong? | Perfect Power? | Trojan? |
|---|---|---|---|---|
| 72 | 2³ × 3² | Yes | No | Yes |
| 108 | 2² × 3³ | Yes | No | Yes |
| 8 | 2³ | Yes | Yes (2³) | No |
| 12 | 2² × 3¹ | No | No | No |
Conclusion
A Trojan number must satisfy two conditions: it must be a strong number (all prime factors appear at least twice) and not be expressible as a perfect power. Use the combined check to identify Trojan numbers correctly.
