from functools import cache
@cache
def solve(n, k):
if n <= 0:
return [[]]
elif n == 1:
return [[1]]
m = 1
ans = []
while m**k <= n:
r = n - m**k
for xs in solve(r, k):
if (len(xs) > 0 and xs[-1] <= m) or len(xs) == 0:
ans.append(xs[:] + [m])
m += 1
return ans
def waring(n, k):
s = solve(n, k)
l = min(len(xs) for xs in s)
return [x for x in s if len(x) == l]
sol = waring(54, 4)
print(sol)
To embed this program on your website, copy the following code and paste it into your website's HTML: