logo

Python-program för att skriva ut Fibonacci-sekvensen

I den här handledningen kommer vi att diskutera hur användaren kan skriva ut Fibonacci-talsekvensen i Python.

Fibonacci-sekvens:

I Fibonacci-sekvensen är 1:a två-talet 1 och 0. Fibonacci-sekvensen specificerar en serie tal där nästa nummer hittas genom att lägga ihop de två talen strax före. Exempel på Fibonacci-serien är 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... och så vidare.

Python-program för att skriva ut Fibonacci-sekvensen

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … och så vidare.

I matematiska termer, sekvensen 'Fn' i Fibonacci-talsekvensen definieras av återfallsrelationen:

java tuplar

Fn= Fn_1+ Fn_2

Där frövärden är:

F0=0 och F1=1

Metod: 1 - Genom att använda en while-loop

Vi kommer att använda en while-loop för att skriva ut sekvensen av Fibonacci-sekvensen.

Steg 1: Mata in antalet värden vi vill generera Fibonacci-sekvensen

kompatibilitetstestning

Steg 2: Initiera antalet = 0, n_1 = 0 och n_2 = 1.

Steg 3: Om n_terms<= 0< p>

Steg 4: skriv ut 'fel' eftersom det inte är ett giltigt nummer för serier

Steg 5: om n_terms = 1, kommer det att skriva ut n_1 värde.

Steg 6: medan räkna

Steg 7: print (n_1)

fmovies

Steg 8: n:e = n_1 + n_2

Steg 9: vi kommer att uppdatera variabeln, n_1 = n_2, n_2 = n:te och så vidare, upp till den obligatoriska termen.

Exempel 1:

Här ger vi ett exempel på hur man skriver ut en Fibonacci-serie i Python. Exemplet ges nedan -

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

Förklaring:

I ovanstående kod har vi lagrat villkoren i n_terms. Vi har initialiserat den första termen som ' 0 ' och den andra termen som ' 1 '. Om antalet termer är fler än 2 kommer vi att använda while-loopen för att hitta nästa term i Fibonacci-sekvensen genom att lägga till de två föregående termerna. Vi kommer sedan att uppdatera variabeln genom att byta ut dem, och den kommer att fortsätta med processen upp till det antal termer som användaren vill skriva ut.

Exempel 2:

Här ger vi ett annat exempel på hur man skriver ut en Fibonacci-serie i Python. Exemplet ges nedan -

 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 

Produktion:

referenspekare

Nu kompilerar vi ovanstående program i Python, och efter kompilering kör vi det. Då ges resultatet nedan -

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

I koden ovan tar vi användarinmatning om hur många termer de vill skriva ut. Sedan initierar vi a och b med 0 och 1. Sedan skapar vi en for-loop. Skriv sedan ut a och b. Efter det initierar vi en variabel c. Lägg sedan till a och b och lagra dem i variabel c. Till sist skriver vi ut värdet på c och sedan är slingan rund till det angivna numret av användaren.

Exempel 3:

Här ger vi ett annat exempel på hur man skriver ut en Fibonacci-serie i Python med hjälp av funktion. Exemplet ges nedan -

java sträng klass
 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) 

Produktion:

Nu kompilerar vi ovanstående program i Python, och efter kompilering kör vi det. Då ges resultatet nedan -

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

Förklaring:

I ovanstående kod skapar vi ett funktionsnamn fibo. Här lägger vi till de två första termerna och lagrar dem nästa. Här använder vi append-syntax för att lagra det och skriva ut det.

Slutsats:

I den här handledningen har vi diskuterat hur användaren kan skriva ut Fibonacci-talsekvensen till den n:e termen. Fibonacci-serien börjar med 0 och 1. Därefter fortsätter serien med addering före ett. Vi ger också några exempel på Fibonacci-serien i Python och delar resultatet av den.