Mercurial > cpython-lru_cache-weakref
annotate Demo/scripts/pi.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 28948181fb2f |
| children | 73758db2f464 |
| rev | line source |
|---|---|
|
1269
28948181fb2f
*** empty log message ***
Guido van Rossum <guido@python.org>
parents:
1001
diff
changeset
|
1 #! /usr/local/bin/python |
| 274 | 2 |
| 3 # Print digits of pi forever. | |
| 4 # | |
| 5 # The algorithm, using Python's 'long' integers ("bignums"), works | |
| 6 # with continued fractions, and was conceived by Lambert Meertens. | |
| 7 # | |
| 8 # See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton, | |
| 9 # published by Prentice-Hall (UK) Ltd., 1990. | |
| 10 | |
| 11 import sys | |
| 12 | |
| 13 def main(): | |
|
1001
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
14 k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L |
| 274 | 15 while 1: |
| 16 # Next approximation | |
|
1001
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
17 p, q, k = k*k, 2L*k+1L, k+1L |
| 274 | 18 a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1 |
| 19 # Print common digits | |
| 20 d, d1 = a/b, a1/b1 | |
| 559 | 21 while d == d1: |
|
1001
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
22 output(d) |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
23 a, a1 = 10L*(a%b), 10L*(a1%b1) |
| 274 | 24 d, d1 = a/b, a1/b1 |
| 25 | |
|
1001
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
26 def output(d): |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
27 # Use write() to avoid spaces between the digits |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
28 # Use int(d) to avoid a trailing L after each digit |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
29 sys.stdout.write(`int(d)`) |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
30 # Flush so the output is seen immediately |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
31 sys.stdout.flush() |
|
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
32 |
| 274 | 33 main() |
