Micro-challenge: Special Event
|
01-04-2023, 06:48 PM
(This post was last modified: 01-05-2023 12:53 AM by Albert Chan.)
Post: #25
|
|||
|
|||
RE: Micro-challenge: Special Event
(01-04-2023 01:56 AM)Thomas Klemm Wrote: Here’s a program for the HP-42S to calculate \(\pi\) ... FYI, 40 meant 40 digits after radix point (total 41 digits) lua> require'fun'() lua> horner = fn's,a,b: s*b+a' lua> terms = fn'n: range(n,1,-1)' lua> pi_atan_euler = fn'x: 2, x/(2*x+1)' x → ∞ produced (2, 1/2): 2*(1/2) + 2 = 3 (2.2222 ...)b last digit = 3 converge to pi faster. lua> terms(40) :map(pi_atan_euler) :reduce(horner, 2) 3.141592653589546 lua> terms(40) :map(pi_atan_euler) :reduce(horner, 3) 3.1415926535896728 Another example, pi = 6*asin(1/2) taylor series. \(\displaystyle \pi = 3 + \left(\frac{1^2}{4×2×3}\right) \left(3 +\; \left(\frac{3^2}{4×4×5}\right) \left(3 +\; \left(\frac{5^2}{4×6×7}\right) \left(3 +\; \cdots \right) \right) \right) \) lua> pi_asin_half = fn'x: x=2*x-1; 3, (x*x)/(4*(x+1)*(x+2))' x → ∞ produced (3, 1/4): 3*(1/4) + 3 = 3.75 (3.3333 ...)b last digit = 3.75 converge to pi faster. (1/4) = (1/2)^2 --> about half terms needed for equivalent accuracy. lua> terms(20) :map(pi_asin_half) :reduce(horner, 3) 3.141592653589791 lua> terms(20) :map(pi_asin_half) :reduce(horner, 3+.75) 3.1415926535897927 Or, we turn this into ratio of "integers", with top digit base = 4*2*3/1^2 = 24. Note: range(20) mean 1 .. 20 lua> base = range(20): map(fn'x: x=2*x-1; (4*(x+1)*(x+2))/(x*x)') lua> n = zip(xrepeat(3), base) :reduce(horner, 3) lua> d = base :product() lua> n/d, (n+.75)/d 3.141592653589791 3.141592653589793 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)