Jump to content

[PYTHON] Display the Fibonacci Sequence


Tupi
 Share

Recommended Posts

Definim functia:

def fibosequence(n):
    if n <= 1:
        return n
    else:
        return(fibosequence(n-1)+fibosequence(n-2))

 

Determinam cati termen vrem sa fie in sir:

terms = 20

 

Verificam daca numarul termenilor este unul natul si nenul, daca conditia este indeplinita vom afisa sirul:

if terms <= 0:
    print("Please enter a positive integer")
else:
    print("fibonacci sequence:")
    for i in range(terms):
        print(fibosequence(i))

 

Rezultatul pentru primii 20 de termeni este urmatorul, acesta difera in functie de cati termeni introducem:

fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

 

Link to comment
Share on other sites

  • Tupi locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.