Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b2b440

Browse files
committed
all Long constants have an L suffix, not l;
added an output() function to move the I/O out of the algorithm
1 parent 0cc1945 commit 0b2b440

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

Demo/scripts/pi.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@
1111
import sys
1212

1313
def main():
14-
k, a, b, a1, b1 = 2l, 4l, 1l, 12l, 4l
14+
k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
1515
while 1:
1616
# Next approximation
17-
p, q, k = k*k, 2l*k+1l, k+1l
17+
p, q, k = k*k, 2L*k+1L, k+1L
1818
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
1919
# Print common digits
2020
d, d1 = a/b, a1/b1
21-
#print a, b, a1, b1
2221
while d == d1:
23-
# Use write() to avoid spaces between the digits
24-
sys.stdout.write(`int(d)`)
25-
# Flush so the output is seen immediately
26-
sys.stdout.flush()
27-
a, a1 = 10l*(a%b), 10l*(a1%b1)
22+
output(d)
23+
a, a1 = 10L*(a%b), 10L*(a1%b1)
2824
d, d1 = a/b, a1/b1
2925

26+
def output(d):
27+
# Use write() to avoid spaces between the digits
28+
# Use int(d) to avoid a trailing L after each digit
29+
sys.stdout.write(`int(d)`)
30+
# Flush so the output is seen immediately
31+
sys.stdout.flush()
32+
3033
main()

0 commit comments

Comments
 (0)