forked from 4dsolutions/Python5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_pi.py
More file actions
33 lines (22 loc) · 647 Bytes
/
gen_pi.py
File metadata and controls
33 lines (22 loc) · 647 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 3 16:08:49 2015
@author: kurner
Another generator example: converging to Pi
https://mail.python.org/pipermail/edu-sig/2015-September/date.html
"""
def pi():
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while True:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a/b, a1/b1
while d == d1:
yield int(d)
a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a/b, a1/b1
if __name__ == "__main__":
the_gen = pi()
for _ in range(20000):
print(next(the_gen),end="")
print()