logo

Python-programma om de Fibonacci-reeks af te drukken

In deze tutorial bespreken we hoe de gebruiker de Fibonacci-reeks getallen in Python kan afdrukken.

Fibonacci-reeks:

In de Fibonacci-reeks is het eerste twee getal 1 en 0. De Fibonacci-reeks specificeert een reeks getallen waarin het volgende getal wordt gevonden door de twee getallen net ervoor op te tellen. Voorbeeld van de Fibonacci-reeks is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... enzovoort.

Python-programma om de Fibonacci-reeks af te drukken

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … enzovoort.

In wiskundige termen is de reeks 'FN' van de Fibonacci-reeks getallen wordt gedefinieerd door de herhalingsrelatie:

c structuur in structuur

FN= Fn_1+ Fn_2

Waar zaadwaarden zijn:

F0=0 en F1=1

javascript base64-decodering

Methode: 1 - Door een while-lus te gebruiken

We zullen een while-lus gebruiken om de reeks van de Fibonacci-reeks af te drukken.

Stap 1: Voer het aantal waarden in waarmee we de Fibonacci-reeks willen genereren

Stap 2: Initialiseer de telling = 0, n_1 = 0 en n_2 = 1.

Stap 3: Als de n_terms<= 0< p>

Stap 4: print 'error' omdat dit geen geldig nummer is voor series

Stap 5: als n_terms = 1, wordt de waarde n_1 afgedrukt.

Stap 6: terwijl tellen

Stap 7: afdrukken (n_1)

ffilms

Stap 8: nde = n_1 + n_2

Stap9: we zullen de variabele bijwerken, n_1 = n_2, n_2 = nde enzovoort, tot aan de vereiste term.

Voorbeeld 1:

Hier geven we een voorbeeld van hoe je een Fibonacci-reeks in Python kunt afdrukken. Het voorbeeld wordt hieronder gegeven -

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

Uitleg:

numpige sommatie

In de bovenstaande code hebben we de termen opgeslagen in n_terms. We hebben de eerste term geïnitialiseerd als ' 0 ' en de tweede term als ' 1 '. Als het aantal termen meer dan 2 is, gebruiken we de while-lus om de volgende term in de Fibonacci-reeks te vinden door de vorige twee termen op te tellen. Vervolgens zullen we de variabele bijwerken door ze uit te wisselen, en het proces zal doorgaan tot het aantal termen dat de gebruiker wil afdrukken.

Voorbeeld 2:

Hier geven we nog een voorbeeld van hoe je een Fibonacci-reeks in Python kunt afdrukken. Het voorbeeld wordt hieronder gegeven -

 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 

Uitgang:

Nu compileren we het bovenstaande programma in Python, en na het compileren voeren we het uit. Vervolgens wordt het resultaat hieronder gegeven -

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

In de bovenstaande code nemen we gebruikersinvoer over hoeveel termen ze willen afdrukken. Vervolgens initialiseren we a en b met 0 en 1. Vervolgens maken we een for-lus. Druk vervolgens a en b af. Daarna initialiseren we een variabele c. Voeg vervolgens a en b toe en sla deze op in variabele c. Ten slotte drukken we de waarde van c af en dan is de lus rond tot het door de gebruiker opgegeven getal.

Java-constante

Voorbeeld 3:

Hier geven we nog een voorbeeld van hoe je een Fibonacci-reeks in Python kunt afdrukken met behulp van de functie. Het voorbeeld wordt hieronder gegeven -

 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) 

Uitgang:

Nu compileren we het bovenstaande programma in Python, en na het compileren voeren we het uit. Vervolgens wordt het resultaat hieronder gegeven -

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

Uitleg:

In de bovenstaande code maken we een functienaam fibo. Hier voegen we de eerste twee termen toe en slaan ze vervolgens op. Hier gebruiken we de append-syntaxis om het op te slaan en af ​​te drukken.

Conclusie:

In deze tutorial hebben we besproken hoe de gebruiker de Fibonacci-reeks getallen kan afdrukken tot de n-de term. De Fibonacci-reeks begint met 0 en 1. Daarna wordt de reeks vervolgd met het optellen vóór één. We geven ook enkele voorbeelden van de Fibonacci-reeks in Python en delen de uitvoer ervan.