logo

Python-Programm zum Drucken der Fibonacci-Folge

In diesem Tutorial besprechen wir, wie der Benutzer die Fibonacci-Zahlenfolge in Python drucken kann.

Fibonacci-Folge:

In der Fibonacci-Folge sind die ersten beiden Zahlen 1 und 0. Die Fibonacci-Folge gibt eine Reihe von Zahlen an, bei der die nächste Zahl durch Addition der beiden unmittelbar davor liegenden Zahlen ermittelt wird. Beispiele für die Fibonacci-Reihe sind 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 usw.

Python-Programm zum Drucken der Fibonacci-Folge

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … und so weiter.

Mathematisch gesehen ist die Folge „FN' der Fibonacci-Zahlenfolge wird durch die Wiederholungsbeziehung definiert:

Verilog-Fallerklärung

FN= Fn_1+ Fn_2

Wobei die Startwerte sind:

F0=0 und F1=1

Methode: 1 – Mithilfe einer While-Schleife

Wir werden eine While-Schleife zum Drucken der Sequenz der Fibonacci-Folge verwenden.

Schritt 1: Geben Sie die Anzahl der Werte ein, die wir für die Fibonacci-Folge generieren möchten

Schritt 2: Initialisieren Sie die Anzahl = 0, n_1 = 0 und n_2 = 1.

was bedeutet das xd

Schritt 3: Wenn die n_terms<= 0< p>

Schritt 4: Geben Sie „Fehler“ aus, da es sich nicht um eine gültige Nummer für die Serie handelt

Schritt 5: Wenn n_terms = 1, wird der Wert n_1 gedruckt.

Schritt 6: während zählen

Schritt 7: drucken (n_1)

fmovies

Schritt 8: nth = n_1 + n_2

Schritt 9: Wir werden die Variable n_1 = n_2, n_2 = nth usw. aktualisieren, bis der erforderliche Term erreicht ist.

Beispiel 1:

Hier geben wir ein Beispiel dafür, wie man eine Fibonacci-Reihe in Python druckt. Das Beispiel ist unten aufgeführt:

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

Erläuterung:

1 von 1000

Im obigen Code haben wir die Begriffe gespeichert n_terms. Wir haben den ersten Term als ' initialisiert 0 ' und der zweite Begriff als ' 1 '. Wenn die Anzahl der Terme mehr als 2 beträgt, verwenden wir die while-Schleife, um den nächsten Term in der Fibonacci-Folge zu finden, indem wir die beiden vorherigen Terme addieren. Anschließend aktualisieren wir die Variablen, indem wir sie austauschen, und der Vorgang wird bis zur Anzahl der Begriffe fortgesetzt, die der Benutzer drucken möchte.

Beispiel 2:

Hier geben wir ein weiteres Beispiel dafür, wie man eine Fibonacci-Reihe in Python druckt. Das Beispiel ist unten aufgeführt:

 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 

Ausgabe:

Jetzt kompilieren wir das obige Programm in Python und führen es nach der Kompilierung aus. Dann ist das Ergebnis unten angegeben -

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

Im obigen Code nehmen wir Benutzereingaben entgegen, um anzugeben, wie viele Begriffe gedruckt werden sollen. Dann initialisieren wir a und b mit 0 und 1. Dann erstellen wir eine for-Schleife. Drucken Sie dann a und b aus. Danach initialisieren wir eine Variable c. Fügen Sie dann a und b hinzu und speichern Sie es in der Variablen c. Zuletzt geben wir den Wert von c aus und dann wird die Schleife bis zur vom Benutzer angegebenen Zahl abgerundet.

Beispiel 3:

Hier geben wir ein weiteres Beispiel dafür, wie man eine Fibonacci-Reihe in Python mithilfe einer Funktion druckt. Das Beispiel ist unten aufgeführt:

 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) 

Ausgabe:

k Nächster-Nachbarn-Algorithmus

Jetzt kompilieren wir das obige Programm in Python und führen es nach der Kompilierung aus. Dann ist das Ergebnis unten angegeben -

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

Erläuterung:

Im obigen Code erstellen wir einen Funktionsnamen fibo. Hier fügen wir die ersten beiden Begriffe hinzu und speichern sie als nächstes. Hier verwenden wir die Append-Syntax, um es zu speichern und auszudrucken.

Abschluss:

In diesem Tutorial haben wir besprochen, wie der Benutzer die Fibonacci-Zahlenfolge bis zum n-ten Term drucken kann. Die Fibonacci-Reihe beginnt mit 0 und 1. Anschließend wird die Reihe mit der Addition vor eins fortgesetzt. Wir geben auch einige Beispiele der Fibonacci-Reihe in Python und teilen deren Ausgabe.