Fibonacci-Folge berechnen
Die folgende Funktion zeigt einen möglichen Lösungsansatz wie man in Python die Fibonacci-Folge errechnen kann. Beim Aufruf der Funktion fibonacci() kann man als Parameter angeben wie viele Zahlen der Folge berechnet werden sollen.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 07.04.2015
#
# Copyright (c) 2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
def fibonacci(n):
a, b = 1, 1
print(a)
for i in range(1,n,1):
print(b)
a, b = b, a+b
if __name__ == '__main__':
fibonacci(20)
# -*- coding: utf-8 -*-
#
# Creation: 07.04.2015
# Last Update: 07.04.2015
#
# Copyright (c) 2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
def fibonacci(n):
a, b = 1, 1
print(a)
for i in range(1,n,1):
print(b)
a, b = b, a+b
if __name__ == '__main__':
fibonacci(20)
Wird das Python-Skript aufgerufen, erfolgt zum Beispiel die folgende Fibonacci-Folge als Ausgabe.
pi@raspberrypi ~ $ python fibonacci.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
Dieser Eintrag wurde am 12.04.2015 erstellt.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_31_2676.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer