logo

Python-ohjelma Fibonacci-sekvenssin tulostamiseen

Tässä opetusohjelmassa keskustelemme siitä, kuinka käyttäjä voi tulostaa Fibonacci-numerosarjan Pythonissa.

Fibonaccin sekvenssi:

Fibonacci-sarjassa kaksi ensimmäistä lukua on 1 ja 0. Fibonacci-sekvenssi määrittää numerosarjan, josta seuraava luku löydetään laskemalla yhteen kaksi juuri sitä edeltävää numeroa. Esimerkki Fibonacci-sarjasta on 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... ja niin edelleen.

Python-ohjelma Fibonacci-sekvenssin tulostamiseen

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … ja niin edelleen.

Matemaattisesti sekvenssi 'FnFibonaccin numerosarjan ' määritellään toistuvuusrelaatiolla:

nuhjuinen piste

Fn= Fn_1+ Fn_2

Missä siemenarvot ovat:

F0=0 ja F1=1

Menetelmä: 1 - Käyttämällä while-silmukkaa

Käytämme while-silmukkaa Fibonacci-sekvenssin sekvenssin tulostamiseen.

Vaihe 1: Syötä arvojen lukumäärä, jotka haluat luoda Fibonacci-sekvenssin

Vaihe 2: Alusta laskenta = 0, n_1 = 0 ja n_2 = 1.

Vaihe 3: Jos n_terms<= 0< p>

Vaihe 4: tulosta 'error', koska se ei ole kelvollinen numero sarjalle

mikä on kirjainkoko sql:ssä

Vaihe 5: jos n_terms = 1, se tulostaa arvon n_1.

Vaihe 6: laskettaessa

Vaihe 7: tulosta (n_1)

fmovies

Vaihe 8: nth = n_1 + n_2

Vaihe 9: Päivitämme muuttujan, n_1 = n_2, n_2 = nth ja niin edelleen, vaadittuun termiin asti.

Esimerkki 1:

Tässä annamme esimerkin Fibonacci-sarjan tulostamisesta Pythonissa. Esimerkki on annettu alla -

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

Selitys:

Yllä olevaan koodiin olemme tallentaneet ehdot n_terms. Olemme alustaneet ensimmäisen termin ' 0 'ja toinen termi ' 1 '. Jos termien määrä on enemmän kuin 2, käytämme while-silmukkaa seuraavan termin etsimiseen Fibonacci-sekvenssistä lisäämällä kaksi edellistä termiä. Päivitämme sitten muuttujan vaihtamalla ne keskenään, ja se jatkaa prosessia käyttäjän tulostamien termien määrään asti.

Esimerkki 2:

konekirjoitus foreach silmukka

Tässä annamme toisen esimerkin Fibonacci-sarjan tulostamisesta Pythonissa. Esimerkki on annettu alla -

 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 

Lähtö:

Nyt käännämme yllä olevan ohjelman Pythonissa, ja kääntämisen jälkeen suoritamme sen. Sitten tulos on alla -

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

Yllä olevaan koodiin otamme käyttäjän syötteen, kuinka monta termiä he haluavat tulostaa. Sitten alustetaan a ja b arvoilla 0 ja 1. Sitten luodaan for-silmukka. Tulosta sitten a ja b. Tämän jälkeen alustetaan muuttuja c. Lisää sitten a ja b ja tallenna se muuttujaan c. Lopuksi tulostetaan c:n arvo ja sitten silmukka pyöristetään annettuun numeroon käyttäjäkohtaisesti.

Esimerkki 3:

Tässä annamme toisen esimerkin Fibonacci-sarjan tulostamisesta Pythonissa funktion avulla. Esimerkki on annettu alla -

 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) 

Lähtö:

Nyt käännämme yllä olevan ohjelman Pythonissa, ja kääntämisen jälkeen suoritamme sen. Sitten tulos on alla -

vasen liitos vs oikea liitos
 10 0 1 1 2 3 5 8 13 21 34 55 

Selitys:

Yllä olevaan koodiin luomme funktion nimen fibo. Tähän lisäämme kaksi ensimmäistä termiä ja tallennamme ne seuraavaksi. Täällä käytämme append-syntaksia sen tallentamiseen ja tulostamiseen.

Johtopäätös:

Tässä opetusohjelmassa olemme keskustelleet siitä, kuinka käyttäjä voi tulostaa Fibonacci-lukusarjan n:nnelle termille. Fibonacci-sarja alkaa nollasta ja 1:stä. Sitten sarjaa jatketaan lisäämällä ennen yhtä. Annamme myös esimerkkejä Fibonacci-sarjasta Pythonissa ja jaamme sen tuotoksen.