for c in xrange(min(int((-1+(1+4*(i+1))**0.5)/2)+1, curr)): # since c^2 <= (i+1)-c, thus c <= (-1+(1+4*(i+1))**0.5)/2 ...