I'm currently working on a homework assignment, and the topic is to derive a Fibonacci sequence from a Pascal triangle. It sounds simple, but I came upon a lot of trouble when trying to use my method.
I basically attempted to add all of the diagonal numbers of a Fibonacci sequence by reproducing a Pascal triangle, saving all of the numbers (within the same row) into a list, and then adding up the diagonal numbers.
#pretend fibnumber exists, the Fibonacci number needed
# input n
fibnumber=144
n = 5
list=[]
m=''
sumslist=[]
for i in range(n):
for j in range(i+1):
d=factorial(i)//(factorial(j)*factorial(i-j))
m+=str(d)
# for new line
list.append(m)
m=''
print()
print(list)
for thing in list:
sum=0
position=0
listposition=list.index(thing)
print(listposition)
if len(thing)>1:
while position!=len(thing)-1:
sum+=float(thing[position])
position+=1
if listposition==0:
break
print(sum)
thing=list[listposition-1]
sumslist.append(sum)
Ignore the fibnumber variable, that is for the second part which I know. When I tried to run my code, I received an error for "index out of range" for line 30, which is the part about "sum+=...". I have attempted many changes and spent a whole day on this, but nothing seems to work. Can someone help me with this problem? Much appreciated!