Post Reply 
[VA] SRC #009 - Pi Day 2021 Special
03-18-2021, 02:44 PM
Post: #31
RE: [VA] SRC #009 - Pi Day 2021 Special
Hi, robve

Thanks for spending the time to add code challenges.
Both codes are calculating pi by 2/(sin(x)/x) at x=pi/2, in 2 different ways.

(a) pi is via Viète's formula:

sin(x)/x = cos(x/2) cos(x/4) cos(x/8) ...

At x = pi/2:
cos(x/2) = cos(pi/4) = √(2) / 2
cos(x/4) = √((1 + cos(x/2))/2) = √(2 + √(2)) / 2
cos(x/8) = √((1 + cos(x/4))/2) = √(2 + √(2 + √(2))) / 2
...

Generated outputs, 2/Q = 2^k * sin(pi/2^k), k = 2, 3, 4, ...
limit(2^k * sin(pi/2^k), k=∞) = 2^k * (pi/2^k) = pi             // sin(ε) ≈ ε

(b) pi is via Wallis products

With roots of sin(x) = 0, ±pi, ±2pi, ±3pi, ..., and limit(sin(x)/x, x=0) = 1:

sin(x)/x = (1-(x/pi)²) * (1-(x/(2pi))²) * (1-(x/(3pi))²) ...

At x=pi/2:
LHS = 2/pi ≈ 0.63662
RHS = (1-1/4) * (1-1/16) * (1-1/36) ... = (1*3)/(2*2) * (3*5)/(4*4) * (5*7)/(6*6) ...

BTW, it is more efficient (and accurate !) to calculate P=sin(x)/x, at x=pi/2:
Bonus: code is shorter, and easier to understand.

10 P=1
20 FOR I=2 TO 1000 STEP 2
30 P=P-P/(I*I)
40 DISP 2/P
50 NEXT I
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC #009 - Pi Day 2021 Special - Albert Chan - 03-18-2021 02:44 PM



User(s) browsing this thread: 1 Guest(s)