Mercurial > cpython-lru_cache-weakref
annotate Demo/scripts/pi.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 41524f9e2a2f |
| children | 33b041350034 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
1 #! /usr/bin/env 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(): | |
|
16704
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
14 k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
15 while 1: |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
16 # Next approximation |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
17 p, q, k = k*k, 2L*k+1L, k+1L |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
18 a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1 |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
19 # Print common digits |
|
49164
41524f9e2a2f
#687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents:
33456
diff
changeset
|
20 d, d1 = a//b, a1//b1 |
|
16704
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
21 while d == d1: |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
22 output(d) |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
23 a, a1 = 10L*(a%b), 10L*(a1%b1) |
|
49164
41524f9e2a2f
#687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents:
33456
diff
changeset
|
24 d, d1 = a//b, a1//b1 |
| 274 | 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): |
|
16704
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
27 # Use write() to avoid spaces between the digits |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
28 # Use str() to avoid the 'L' |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
29 sys.stdout.write(str(d)) |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
30 # Flush so the output is seen immediately |
|
0468596e3018
Changed to use the fact that str(long) doesn't produce a trailing L
Moshe Zadka <moshez@math.huji.ac.il>
parents:
4573
diff
changeset
|
31 sys.stdout.flush() |
|
1001
5067f2d38e14
all Long constants have an L suffix, not l;
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
32 |
|
33456
487280cf71b9
Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents:
16704
diff
changeset
|
33 if __name__ == "__main__": |
|
487280cf71b9
Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents:
16704
diff
changeset
|
34 main() |
