Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Transfusion/f01d5de95a6e9b19c76b07dd6a2352ec to your computer and use it in GitHub Desktop.
Save Transfusion/f01d5de95a6e9b19c76b07dd6a2352ec to your computer and use it in GitHub Desktop.
import math
def decompose(n):
res = None
ss = []
def rec(squared, next_number_cannot_be_larger_than):
nonlocal res
if res: return
if squared == 0:
res = ss[::-1]
return
start_cond = min(int(math.sqrt(squared)), next_number_cannot_be_larger_than)
for i in range(start_cond, 0, -1):
if res: return
sq2 = i**2
if squared >= sq2:
ss.append(i)
rec(squared - sq2, i-1)
ss.pop()
rec(n**2, n-1)
return res if res else None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment