logo

Programul Python pentru a imprima secvența Fibonacci

În acest tutorial, vom discuta despre modul în care utilizatorul poate imprima succesiunea de numere Fibonacci în Python.

Secvența Fibonacci:

În secvența Fibonacci, primul număr este 1 și 0. Secvența Fibonacci specifică o serie de numere în care următorul număr este găsit prin adunarea celor două numere chiar înainte. Un exemplu de serie Fibonacci este 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... și așa mai departe.

Programul Python pentru a imprima secvența Fibonacci

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … și așa mai departe.

În termeni matematici, succesiunea 'Fn' din șirul de numere Fibonacci este definit de relația de recurență:

Fn= Fn_1+ Fn_2

Unde valorile semințelor sunt:

F0=0 și F1=1

convertiți șirul în int

Metodă: 1 - Prin utilizarea unei bucle while

Vom folosi o buclă while pentru imprimarea secvenței secvenței Fibonacci.

Pasul 1: Introduceți numărul de valori pe care dorim să generăm șirul Fibonacci

Pasul 2: Inițializați numărul = 0, n_1 = 0 și n_2 = 1.

Pasul 3: Dacă n_termeni<= 0< p>

Pasul 4: tipăriți „eroare” deoarece nu este un număr valid pentru serie

Pasul 5: dacă n_terms = 1, va imprima valoarea n_1.

Pasul 6: în timp ce numără

Pasul 7: imprimare (n_1)

Pasul 8: nth = n_1 + n_2

minim maxim

Pasul 9: vom actualiza variabila, n_1 = n_2, n_2 = n-a și așa mai departe, până la termenul necesar.

Exemplul 1:

Aici oferim un exemplu despre cum să tipăriți o serie Fibonacci în Python. Exemplul este dat mai jos -

 n_terms = int(input (&apos;How many terms the user wants to print? &apos;)) # First two terms n_1 = 0 n_2 = 1 count = 0 # Now, we will check if the number of terms is valid or not if n_terms <= 0: print ('please enter a positive integer, the given number is not valid') # if there only one term, it will return n_1 elif n_terms="=" 1: ('the fibonacci sequence of numbers up to', n_terms, ': ') print(n_1) then we generate else: is:') while count < n_terms: nth="n_1" + n_2 at last, update values pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -</p> <pre>How many terms the user wants to print? 13 The Fibonacci sequence of the numbers is: 0 1 1 2 3 5 8 13 21 34 55 89 144 </pre> <p> <strong>Explanation:</strong> </p> <p>In the above code, we have stored the terms in <strong>n_terms.</strong> We have initialized the first term as &apos; <strong>0</strong> &apos; and the second term as &apos; <strong>1</strong> &apos;. If the number of terms is more than 2, we will use the while loop for finding the next term in the Fibonacci sequence by adding the previous two terms. We will then update the variable by interchanging them, and it will continue with the process up to the number of terms the user wants to print.</p> <p> <strong>Example 2:</strong> </p> <p>Here we give another example that how to print a Fibonacci series in Python. The example is given below -</p> <pre> n = int(input (&apos;Enter the number you want to print: &apos;)) # Take input from user that how many numbers you want to print a = 0 b = 1 for i in range(0,n): print(a, end = &apos; &apos;) # a:0; a:1; a:2 c = a+b #c=0+1=1; c= 1+1=2; c=1+2=3 a = b #a=1 ; a=1; a=2 b = c #b=1 ; b=2; b=3 </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -</p> <pre> Enter the number you want to print: 10 0 1 1 2 3 5 8 13 21 34 </pre> <p>In the above code we take user input that how many terms they want to print. Then we initialize a and b with 0 and 1. Then we create a for loop. Then print a and b. After that we initialize a variable c. Then add a and b and store it in variable c. At last, we print the value of c and then the loop is round till the given number by user.</p> <p> <strong>Example 3:</strong> </p> <p>Here we give another example that how to print a Fibonacci series in Python using function. The example is given below -</p> <pre> def Fibo(Term): values = [] First = 0 Second = 1 Next = First + Second values.append(First) values.append(Second) for i in range(2,Term+1): values.append(Next) First = Second Second = Next Next = First + Second return values Term = int(input()) res=Fibo(Term) print(*res) </pre> <p> <strong>Output:</strong> </p> <p>Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -</p> <pre> 10 0 1 1 2 3 5 8 13 21 34 55 </pre> <p> <strong>Explanation:</strong> </p> <p>In the above code, we create a function name fibo. Here we add 1st two terms and store them next. Here we use append syntax to store it and print it.</p> <h2>Conclusion:</h2> <p>In this tutorial, we have discussed how the user can print the Fibonacci sequence of numbers to the nth term. The Fibonacci series starts with 0 and 1. Then the series is continued with adding before one. We also give some examples of the Fibonacci series in Python and share the output of it.</p> <hr></=>

Explicaţie:

În codul de mai sus, am stocat termenii în n_termeni. Am inițializat primul termen ca „ 0 ' și al doilea termen ca ' 1 '. Dacă numărul de termeni este mai mare de 2, vom folosi bucla while pentru a găsi următorul termen din șirul Fibonacci prin adăugarea celor doi termeni anteriori. Vom actualiza apoi variabila prin interschimbarea lor și va continua cu procesul până la numărul de termeni pe care utilizatorul dorește să-i imprime.

Exemplul 2:

Aici dăm un alt exemplu despre cum să tipăriți o serie Fibonacci în Python. Exemplul este dat mai jos -

 n = int(input (&apos;Enter the number you want to print: &apos;)) # Take input from user that how many numbers you want to print a = 0 b = 1 for i in range(0,n): print(a, end = &apos; &apos;) # a:0; a:1; a:2 c = a+b #c=0+1=1; c= 1+1=2; c=1+2=3 a = b #a=1 ; a=1; a=2 b = c #b=1 ; b=2; b=3 

Ieșire:

Acum compilam programul de mai sus în Python și, după compilare, îl rulăm. Apoi rezultatul este dat mai jos -

fmovies
 Enter the number you want to print: 10 0 1 1 2 3 5 8 13 21 34 

În codul de mai sus, luăm intrarea utilizatorului pentru câți termeni doresc să imprime. Apoi inițializam a și b cu 0 și 1. Apoi creăm o buclă for. Apoi tipăriți a și b. După aceea inițializam o variabilă c. Apoi adăugați a și b și stocați-l în variabila c. În cele din urmă, imprimăm valoarea lui c și apoi bucla este rotundă până la numărul dat de utilizator.

Exemplul 3:

Aici dăm un alt exemplu despre cum să tipăriți o serie Fibonacci în Python folosind funcția. Exemplul este dat mai jos -

 def Fibo(Term): values = [] First = 0 Second = 1 Next = First + Second values.append(First) values.append(Second) for i in range(2,Term+1): values.append(Next) First = Second Second = Next Next = First + Second return values Term = int(input()) res=Fibo(Term) print(*res) 

Ieșire:

Acum compilam programul de mai sus în Python, iar după compilare, îl rulăm. Apoi rezultatul este dat mai jos -

 10 0 1 1 2 3 5 8 13 21 34 55 

Explicaţie:

În codul de mai sus, creăm un nume de funcție fibo. Aici adăugăm primii doi termeni și apoi îi stocăm. Aici folosim sintaxa append pentru a o stoca și a o tipări.

Concluzie:

În acest tutorial, am discutat despre modul în care utilizatorul poate imprima succesiunea de numere Fibonacci la al n-lea termen. Seria Fibonacci începe cu 0 și 1. Apoi seria se continuă cu adăugarea înainte de unu. De asemenea, dăm câteva exemple ale seriei Fibonacci în Python și împărtășim rezultatul acesteia.