Skip to content

Commit

Permalink
Wrap Python code into a function
Browse files Browse the repository at this point in the history
To address the concerns in #8 about Python, this should improve things.
  • Loading branch information
niklas-heer committed Sep 28, 2022
1 parent 48326ae commit 2b88aff
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/leibniz.py
Expand Up @@ -2,17 +2,20 @@
# coding=utf-8
import os

# https://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function
def main():
with open("rounds.txt") as file:
rounds = int(file.read())

with open("rounds.txt") as file:
rounds = int(file.read())
x = 1.0
pi = 1.0

x = 1.0
pi = 1.0
for i in range(2, rounds + 2):
x *= -1
pi += x / (2 * i - 1)

for i in range(2, rounds + 2):
x *= -1
pi += x / (2 * i - 1)
pi *= 4

pi *= 4

print("{:.16f}".format(pi))
print("{:.16f}".format(pi))
main()

0 comments on commit 2b88aff

Please sign in to comment.