|
Last change
on this file was 388, checked in by dmik, 12 years ago |
|
python: Update vendor to 2.7.6.
|
-
Property svn:eol-style
set to
native
|
|
File size:
888 bytes
|
| Line | |
|---|
| 1 | #! /usr/bin/env python
|
|---|
| 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():
|
|---|
| 14 | k, a, b, a1, b1 = 2, 4, 1, 12, 4
|
|---|
| 15 | while True:
|
|---|
| 16 | # Next approximation
|
|---|
| 17 | p, q, k = k*k, 2*k+1, k+1
|
|---|
| 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
|
|---|
| 21 | while d == d1:
|
|---|
| 22 | output(d)
|
|---|
| 23 | a, a1 = 10*(a%b), 10*(a1%b1)
|
|---|
| 24 | d, d1 = a//b, a1//b1
|
|---|
| 25 |
|
|---|
| 26 | def output(d):
|
|---|
| 27 | # Use write() to avoid spaces between the digits
|
|---|
| 28 | sys.stdout.write(str(d))
|
|---|
| 29 | # Flush so the output is seen immediately
|
|---|
| 30 | sys.stdout.flush()
|
|---|
| 31 |
|
|---|
| 32 | if __name__ == "__main__":
|
|---|
| 33 | main()
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.