Re: [math-fun] Short programs for e, etc. (was Re: Factorial base)
rcs@xmission.com, he say,
The program, in assembly language, easily fit on one page. A gem. What's fun has something to do with the right level of challenge. For me this has been one of the most fun items on the list in a while. Anyway, in Python... import math print repr(math.e), "= math.e"
from sys import stdout
stdout.write("2.") # 32! is about 10**35. d[0] and d[1] are unused. d = [1] * 33 for i in range(35): c = 0 for j in range(len(d) - 1, 1, -1): # len(d)-1 down to 2. c, d[j] = divmod(d[j] * 10 + c, j) stdout.write(str(c)) # (Prints a digit without adding a space.) print It's cache-friendly as-is.
It's a pipeline so it's nicely parallelizable. I think I could do sin(1) and sin(3) but not sin(3 + sin(3)) this way. You can speed it up by a factor of four by converting to base 10**4. ...for that matter, why not generate all the digits in one pass? --Steve
Stanley Rabinowitz, Stan Wagon, A Spigot Algorithm for the Digits of $\pi$, The American Mathematical Monthly, vol.102, no.3, pp.195-203, (March-1995).
http://www.mathpropress.com/stan/bibliography/ Unbounded Spigot Algorithms for the Digits of Pi
http://www.cs.ox.ac.uk/jeremy.gibbons/publications/spigot.pdf --Steve
participants (1)
-
Steve Witham