a = [0, 1, 2] dic = {(2, 1): 1} maxx = 1000 def get(x, y): if y >= x: return a[x] return dic.get((x, y)) for n in xrange(3, maxx + 1): tmp = 0 for i in xrange(1, n): tmp += get(n - i, i) dic.update({(n, i): tmp}) a.append(tmp + 1) for i in xrange(len(a)): if a[i] % 10 == 0: print i, a[i]