Powerful Integers in Python

A powerful integer is defined as a number that can be expressed in the form xi + yj where x and y are positive integers, and i, j are non-negative integers. Given two positive integers x and y along with a bound, we need to find all powerful integers less than or equal to the bound.

Understanding Powerful Integers

For example, with x = 2, y = 3, and bound = 10:

  • 2 = 20 + 30 = 1 + 1
  • 3 = 21 + 30 = 2 + 1
  • 4 = 20 + 31 = 1 + 3
  • 5 = 21 + 31 = 2 + 3
  • 7 = 22 + 31 = 4 + 3
  • 9 = 23 + 30 = 8 + 1
  • 10 = 20 + 32 = 1 + 9

Algorithm Approach

The solution handles different cases based on the values of x and y:

  • If both x and y equal 1: Only 2 (10 + 10) is possible
  • If x equals 1: Generate powers of y and add 1
  • If y equals 1: Generate powers of x and add 1
  • Otherwise: Generate all combinations of xi + yj

Implementation

def powerfulIntegers(x, y, bound):
    result = set()
    
    # Generate all combinations of x^i + y^j
    i = 0
    while x ** i <= bound:
        j = 0
        while x ** i + y ** j <= bound:
            result.add(x ** i + y ** j)
            j += 1
            # If y == 1, y^j will always be 1, so break to avoid infinite loop
            if y == 1:
                break
        i += 1
        # If x == 1, x^i will always be 1, so break to avoid infinite loop
        if x == 1:
            break
    
    return sorted(list(result))

# Test the function
print(powerfulIntegers(2, 3, 10))
print(powerfulIntegers(2, 1, 10))
print(powerfulIntegers(1, 2, 5))
[2, 3, 4, 5, 7, 9, 10]
[2, 3, 5, 9]
[2, 3, 5]

Step-by-Step Breakdown

The algorithm works by:

  1. Using a set to automatically handle duplicates
  2. Nested loops to generate all combinations of xi and yj
  3. Breaking conditions to prevent infinite loops when x or y equals 1
  4. Boundary checking to ensure we don't exceed the bound

Alternative Cleaner Implementation

def powerfulIntegers(x, y, bound):
    result = set()
    
    # Limit iterations to prevent infinite loops
    max_power = 20  # 2^20 > 10^6, sufficient for most bounds
    
    for i in range(max_power):
        if x ** i > bound:
            break
        for j in range(max_power):
            powerful_num = x ** i + y ** j
            if powerful_num > bound:
                break
            result.add(powerful_num)
            if y == 1:  # y^j will always be 1
                break
        if x == 1:  # x^i will always be 1
            break
    
    return sorted(list(result))

# Test with different inputs
test_cases = [(2, 3, 10), (2, 1, 10), (1, 1, 5), (3, 5, 15)]

for x, y, bound in test_cases:
    result = powerfulIntegers(x, y, bound)
    print(f"x={x}, y={y}, bound={bound}: {result}")
x=2, y=3, bound=10: [2, 3, 4, 5, 7, 9, 10]
x=2, y=1, bound=10: [2, 3, 5, 9]
x=1, y=1, bound=5: [2]
x=3, y=5, bound=15: [2, 4, 6, 10, 14]

Key Points

  • Use a set to automatically eliminate duplicate powerful integers
  • Handle edge cases where x or y equals 1 to avoid infinite loops
  • Break early when the current power exceeds the bound
  • Sort the final result for consistent output

Conclusion

Powerful integers can be efficiently found using nested loops with proper boundary conditions. The key insight is handling edge cases when x or y equals 1 and using a set to eliminate duplicates automatically.

Updated on: 2026-03-25T08:56:56+05:30

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements