from math import sqrt, log10 formula = {3:((1,1,0),(2,1),2), 5:((3,-1,0),(6,-1),2), 6:((2,-1,0),(4,-1),1), 7:((5,-3,0),(10,-3),2), 8:((3,-2,0),(6,-2),1)} def poly(x, coef): out = 0 for i in coef: out = x * out + i return out def tt_ori(x, f, fd, flag): tmp = int(sqrt(flag * x)) tmp_ = 0 while tmp_ != tmp: tmp_ = tmp tmp -= (poly(tmp, f) - flag * x) / poly(tmp, fd) while poly(tmp, f) - flag * x > 0: tmp -= 1 return tmp + 1 def tt(x, num): return tt_ori(x, formula.get(num)[0], formula.get(num)[1], formula.get(num)[2]) def test(x, num): return poly(x, formula.get(num)[0]) / formula.get(num)[2] def sumri(num, n): out = [] tmp = tt(num, n) while test(tmp, n) < num + 100: out.append(tmp) tmp += 1 return out def iter(num, lis, end, trap): for i in lis: trynum = sumri(num, i) for j in trynum: tmp = test(j, i) if str(tmp)[2] == '0': continue if len(lis) == 1 and str(tmp)[2:] == end: trap.append((tmp,j,i)) total = 0 for i in trap: total += i[0] print total print trap quit() lis_ = lis[:] lis_.remove(i) trap.append((tmp, j,i)) iter(tmp % 100 * 100, lis_, end, trap) trap.pop(-1) def main(): for i in xrange(32, 100): tmp = i ** 2 if str(tmp)[2] == '0': continue ch = str(tmp)[0:2] iter(tmp % 100 * 100, [3,5,6,7,8], ch, [(tmp,i,4)]) main()