Powerful integers

December 18, 2022

math-and-geometry

Problem URL: Powerful integers

We can use brute force to calculate all the possible values of x^i + y^j until the value is less than the bound and store them in a set. Then we can return the set as a list.

class Solution:
    def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
        num = set()
        for i in range(0, 100):
            for j in range(0, 100):
                a = x**i + y**j
                if a <= bound:
                    num.add(a)
                else:
                    break
        return list(num)

Time complexity: O(n^2)
Space complexity: O(n)