-2

I want to execute the first 20 entries of the Fibonacci Sequence.

Like this 1,1,2,3,5,8.... with 4 entries per line.

I tried the following code:

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

fib(20)

1 Answers1

2
def fib(n):
    a=[1,1]
    for i in range(2,n):
        a.append(a[i-1]+a[i-2])
    return a

a=fib(20)
for i in range(20):
    print(a[i],end=" ")
    if(i+1)%4==0:
        print()