DECLARE SUB MakeHeader () DECLARE SUB PrintOutcome (n!) DECLARE FUNCTION ExactExpression! (n!) DECLARE FUNCTION Simulated! (n!, Ns!) DECLARE FUNCTION Bin! (n!, p!) DECLARE SUB cprint (T$) DECLARE SUB separator () DECLARE SUB IntroduceProblem () DECLARE SUB waitForKey () IntroduceProblem MakeHeader FOR n = 25 TO 200 STEP 50 PrintOutcome 2 * n NEXT n cprint "A PUZZLE: what is the misterious constant in the right hand side column?" waitForKey cprint "Answer: 1/ûã÷" + STR$(1 / SQR(3.1415926#)) END FUNCTION Bin (n, p) 'simualte one outcome of binomial experiment FOR j = 1 TO n IF RND(1) < p THEN x = x + 1 NEXT Bin = x END FUNCTION SUB cprint (T$) 'center text l = 41 - LEN(T$) \ 2 IF l < 0 THEN l = 0 PRINT TAB(l); T$ END SUB FUNCTION ExactExpression (n) 'probability of hitting n out of 2n p = 1 FOR j = 1 TO n p = p / 2 * (2 * j - 1) / j NEXT j ExactExpression = p END FUNCTION SUB IntroduceProblem 'introduce the issue CLS cprint "This program shows what answers can we get from a computer" cprint "W. Bryc January 1996" separator cprint "QUESTION: How often do we get exactly 50% of Heads?" PRINT "Probability theory says "; " that in 2n tosses the "; " chance is (2n)!/(n!)^2 4^(-n)"; PRINT " This program"; " verifies"; " whether"; " this is supported by simulation,"; " and how"; " does"; " this value "; " change "; "with n."; PRINT " The latter"; " is "; "accomplished "; "by "; "computing "; "ûn times "; "the "; "probability!" waitForKey END SUB SUB MakeHeader 'heading for printout PRINT "2n"; TAB(20); "Pr(X=n)"; TAB(40); "Simulated"; TAB(60); "ûn Pr(X=n)" END SUB SUB PrintOutcome (n) 'Print outcomes PRINT 2 * n, USING "#.#####"; TAB(20); ExactExpression(n); TAB(40); Simulated(n, 1000); TAB(60); SQR(n) * ExactExpression(n)'1 / SQR(3.1415926# * n / 2) END SUB SUB separator PRINT STRING$(80, "Ä") END SUB FUNCTION Simulated (n, Ns) 'simulate probability in n-trials repeated Ns times FOR j = 1 TO Ns IF Bin(2 * n, 1 / 2) = n THEN x = x + 1 NEXT j Simulated = x / Ns END FUNCTION SUB waitForKey 'wait for user cprint "Press any key to continue..." WHILE INKEY$ = "" WEND LOCATE CSRLIN - 1, 1 separator END SUB