Skip to content

Commit 5f2996f

Browse files
author
Björn Kötter
committed
optimize code
1 parent e2db28d commit 5f2996f

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

openSAP/exercise-bonus-week6.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import math
2-
import random
1+
from math import pi
2+
from random import random
33

4-
points_in_square = [(random.random(), random.random()) for _ in range(10000)]
5-
count_in_circle = len([(x, y) for x, y in points_in_square if x * x + y * y < 1])
6-
my_pi = count_in_circle / len(points_in_square) * 4
4+
my_pi = len([True for _ in range(10000) if random() ** 2 + random() ** 2 < 1]) / 10000 * 4
75

86
print(f'Calculated value of π: {my_pi}')
97

10-
print(f'Value of π from math library: {math.pi}')
11-
print(f'Difference: {abs(math.pi - my_pi)}')
8+
print(f'Value of π from math library: {pi}')
9+
print(f'Difference: {abs(pi - my_pi)}')

0 commit comments

Comments
 (0)