<em>Bad Code</em>

Fibonacci Sequence & Pascal’s Triangle | <em>Bad Code</em> [2]

The first 15 rows of Pascal's Triangle

 

Two short pieces of code – one for generating the first n terms of the Fibonacci Sequence, the other for generating the first n rows of Pascal’s Triangle.

 

 

#Function to generate the Fibonacci sequence to the nth term
index = int(input("Please enter the number of terms in the Fibonacci sequence to display:\n"))
f0 = 0
f1 = 1
ff = 0
print("1:1")
for i in range(1,index):
    ff = f0 + f1
    f0 = f1
    f1 = ff
    print(str(i+1) + ":"+ str(ff))


#Function to generate PAscal's triangle to the nth row
initial = [1]

n = int(input("Please enter the number of rows to print:\n"))

for i in range(0,n):
    print(initial)
    new_numbers = []
    new_numbers.append(initial[0])
    for j in range(0,i):
        new_numbers.append(initial[j] + initial[j+1])
    new_numbers.append(initial[-1])
    initial = new_numbers

 

Leave a Reply

Your email address will not be published. Required fields are marked *