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 any permutation of N equals any power of K in Python
This problem asks us to determine if any permutation of the digits of number n equals some power of number m. For example, given n = 7182 and m = 12, we find that 1728 (a permutation of 7182) equals 12³.
Approach
We'll generate all powers of m within our range, then check if any power has the same digit frequency as n using sets for comparison ?
Helper Function to Check Digit Permutation
First, we create a function to check if two numbers have the same digits ?
def check_power(n, m):
temp_arr_1 = []
temp_arr_2 = []
# Extract digits from n
while n > 0:
temp_arr_1.append(n % 10)
n //= 10
# Extract digits from m
while m > 0:
temp_arr_2.append(m % 10)
m //= 10
# Compare digit sets
if set(temp_arr_1) == set(temp_arr_2):
return True
return False
Main Solution
Now we generate all powers of m and check each one ?
def solve(n, m):
power_array = [0] * 100
max_range = pow(10, 18)
power_array[0] = m
i = 1
# Generate powers of m
while power_array[i - 1] * m < max_range:
power_array[i] = power_array[i - 1] * m
i += 1
# Check each power
for j in range(i):
if check_power(n, power_array[j]):
return "An all digit-permutation of n is equal to a power of m"
return "No all digit-permutation of n is equal to a power of m"
# Test the function
n, m = 7182, 12
print(solve(n, m))
An all digit-permutation of n is equal to a power of m
Complete Example with Multiple Test Cases
def check_power(n, m):
temp_arr_1 = []
temp_arr_2 = []
while n > 0:
temp_arr_1.append(n % 10)
n //= 10
while m > 0:
temp_arr_2.append(m % 10)
m //= 10
# Use sorted lists for more accurate comparison
return sorted(temp_arr_1) == sorted(temp_arr_2)
def solve(n, m):
power_array = [0] * 100
max_range = pow(10, 18)
power_array[0] = m
i = 1
while power_array[i - 1] * m < max_range:
power_array[i] = power_array[i - 1] * m
i += 1
for j in range(i):
if check_power(n, power_array[j]):
return f"Yes: {power_array[j]} is a permutation of {n}"
return f"No permutation of {n} equals any power of {m}"
# Test cases
test_cases = [(7182, 12), (1234, 2), (8, 2)]
for n, m in test_cases:
print(f"n={n}, m={m}: {solve(n, m)}")
n=7182, m=12: Yes: 1728 is a permutation of 7182 n=1234, m=2: No permutation of 1234 equals any power of 2 n=8, m=2: Yes: 8 is a permutation of 8
How It Works
The algorithm works by:
- Generating Powers: Creates all powers of m within the range limit
- Digit Extraction: Extracts digits from both numbers using modulo operations
- Comparison: Uses sorted lists to check if digits match exactly
- Early Return: Returns immediately when a match is found
Conclusion
This solution efficiently checks if any digit permutation of n equals a power of m by generating all valid powers and comparing digit frequencies. The time complexity depends on the number of powers of m within the given range.
