Recently, I came across a really interesting question:
Given the number N, how many combinations exist that can be written as the sum of several distinct squared numbers?
For example, 25 can be written as:
25 = [5**2 , 3**2 + 4**2]
So the answer would be 2.
But for 31, there exist no solutions, so the answer would be 0.
At the beginning, I thought this would be an easy problem to solve. I just assumed that if I count every combination of squared numbers less than square root of number N, then I should be fine, time-wise. (I found the combinations based on this)
def combs(a):
    if len(a) == 0:
        return [[]]
    cs = []
    for c in combs(a[1:]):
        cs += [c, c+[a[0]]]
    return cs
def main(n):
    k = 0
    l = combs(range(1, int(n**0.5)+1))
    for i in l:
        s = 0
        for j in i:
            s += j**2
        if s == n:
            print(i, n)
            k += 1
    print('\nanswer: ',k)
if __name__ == '__main__':
    main(n = 25)
However, if you replicate my solution, you'll see that after N > 400, the problem gets basically impossible to solve in a short time. So, I was wondering if any optimization exist for this problem?
 
     
     
     
    
