Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created March 15, 2020 02:56
Show Gist options
  • Save ahmed4end/6c7e43d525524b49c837ad693370522d to your computer and use it in GitHub Desktop.
Save ahmed4end/6c7e43d525524b49c837ad693370522d to your computer and use it in GitHub Desktop.
Square into Squares. Protect trees!
def decompose(n):
def rec(n, i):
if n<0:return None
if n==0:return []
for j in range(i-1,0,-1):
hold = rec(n-j**2, j)
if hold!=None:return hold+[j]
return rec(n**2, n)
print(decompose(11))
#>>> [1, 2, 4, 10]
#11**2 = 121 = 1**2 + 2**2 + 4**2 + 10**2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment