The distance is maybe better represented as a function than as an array:
D(I, J) = I + J
where I
and J
are the (un-Pythonesuqe) one-based indices. (Do you realise that in your code, the distances are all zero?)
Also, you should probably be thinking in terms of your number of rows (128) rather than in terms of overall number of values (8128). The purpose of your three iterations is not clear to me. Shouldn't you have loops over rows and columns?
Anyway:
N = 128
n = 2
for i in range(N):
m = n
s = []
for j in range(N - i):
s.append(m)
m += (j + 1) + (i + 1)
print i + 1, s
n += i + 1
You can solve this in another way by noticing that every number occurs only once and follows a pattern:
2 4 7 11
/ / /
/ / /
/ / /
3 6 10
/ /
/ /
/ /
5 9
/
/
/
8
Then you can create all lists up front and print them in a second loop:
n = 2
L = []
for i in range(N):
L.append([])
for LL in reversed(L):
LL.append(n)
n += 1
for i, LL in enumerate(L):
print i + 1, LL