p=[i*(3*i-1)/2 for i in range(10001)] q=set(p) print [[p[j]-p[i],i,j,p[i],p[j]] for i in range(1,5001) for j in range(i+1,5001) if p[j]+p[i] in q and p[j]-p[i] in q]