logo

Programa Python per imprimir la seqüència de Fibonacci

En aquest tutorial, parlarem de com l'usuari pot imprimir la seqüència de nombres de Fibonacci en Python.

Seqüència de Fibonacci:

A la seqüència de Fibonacci, el primer dos nombre és 1 i 0. La seqüència de Fibonacci especifica una sèrie de nombres on es troba el següent nombre sumant els dos nombres just abans. Un exemple de la sèrie de Fibonacci és 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... i així successivament.

nick pulos llamp negre
Programa Python per imprimir la seqüència de Fibonacci

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... i així successivament.

En termes matemàtics, la seqüència 'Fn' de la seqüència de nombres de Fibonacci es defineix per la relació de recurrència:

Fn= Fn_1+ Fn_2

On els valors de les llavors són:

F0=0 i F1=1

Mètode: 1 - Utilitzant un bucle while

Utilitzarem un bucle while per imprimir la seqüència de la seqüència de Fibonacci.

Pas 1: Introduïu el nombre de valors que volem generar la seqüència de Fibonacci

Pas 2: Inicialitzar el recompte = 0, n_1 = 0 i n_2 = 1.

Pas 3: Si els n_terms<= 0< p>

Pas 4: imprimiu 'error' ja que no és un número vàlid per a la sèrie

Pas 5: si n_terms = 1, imprimirà n_1 valor.

Pas 6: mentre compta

Pas 7: imprimir (n_1)

Pas 8: nth = n_1 + n_2

Pas 9: actualitzarem la variable, n_1 = n_2, n_2 = nè i així successivament, fins al terme requerit.

Exemple 1:

Aquí donem un exemple de com imprimir una sèrie de Fibonacci en Python. L'exemple es presenta a continuació -

 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></=>

Explicació:

Al codi anterior, hem emmagatzemat els termes a n_termes. Hem inicialitzat el primer terme com a ' 0 ' i el segon terme com ' 1 '. Si el nombre de termes és superior a 2, utilitzarem el bucle while per trobar el següent terme de la seqüència de Fibonacci sumant els dos termes anteriors. A continuació, actualitzarem la variable intercanviant-les, i continuarà amb el procés fins al nombre de termes que l'usuari vulgui imprimir.

Exemple 2:

Aquí donem un altre exemple de com imprimir una sèrie de Fibonacci en Python. L'exemple es presenta a continuació -

com convertir char a cadena
 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 

Sortida:

Ara compilem el programa anterior en Python i, després de la compilació, l'executem. Aleshores el resultat es mostra a continuació -

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

Al codi anterior prenem l'entrada de l'usuari quants termes volen imprimir. Llavors inicialitzem a i b amb 0 i 1. Després creem un bucle for. A continuació, imprimiu a i b. Després d'això, inicialitzem una variable c. A continuació, afegiu a i b i emmagatzemeu-lo a la variable c. Finalment, imprimim el valor de c i després el bucle s'arrodoneix fins al número donat per l'usuari.

Exemple 3:

Aquí donem un altre exemple de com imprimir una sèrie de Fibonacci a Python mitjançant la funció. L'exemple es presenta a continuació -

 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) 

Sortida:

Ara compilem el programa anterior en Python i, després de la compilació, l'executem. Aleshores el resultat es mostra a continuació -

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

Explicació:

Al codi anterior, creem un nom de funció fibo. Aquí afegim els dos primers termes i els guardem a continuació. Aquí fem servir la sintaxi d'adjuntar per emmagatzemar-la i imprimir-la.

Conclusió:

En aquest tutorial, hem comentat com l'usuari pot imprimir la seqüència de nombres de Fibonacci a l'enè terme. La sèrie de Fibonacci comença amb 0 i 1. A continuació, la sèrie es continua sumant abans d'un. També donem alguns exemples de la sèrie de Fibonacci a Python i en compartim la sortida.