Ich bin gerade dabei, ein einfaches Programm zu schreiben, welches Pi auf einen gewisse Anzahl Nachkommastellen berechnet. Allerdings klappt das ganze nicht so ganz, wie ich möchte:
Wenn man Pi auf 10 Stellen berechnen lässt, kommt -0.106148364023 raus, anstatt 3.1415926535...
Das ganze wurde mit dem Gauss-Legendre-Algorithmus gelöst.
Hier ist der Quellcode:
Code: Alles auswählen
import math
an = 1
bn = 1 / math.sqrt(2)
tn = 1 / 4
pn = 1
bn = int(input("Auf wieviele Stellen soll Pi berechnet werden? "))
for x in range(an, bn, 1):
an = an + 1
an = (an + bn) / 2
bn = bn + 1
bn = math.sqrt(an * bn)
tn = tn + 1
tn = tn - pn * math.pow((an - (an + 1)), 2)
pn = pn + 1
pn = 2 * pn
pi = (math.pow((an + bn), 2)) / (4 * tn)
print(pi)
Danke im voraus.