[VA] SRC #012a - Then and Now: Probability
|
10-16-2022, 05:28 PM
(This post was last modified: 10-16-2022 06:08 PM by C.Ret.)
Post: #48
|
|||
|
|||
RE: [VA] SRC #012a - Then and Now: Probability
The latest version for HP-71B proposed by Chan knocks the beast out.
The probability P( r=30 s=60 ) = 9.51234350207E-6 is displayed in just 31’27.4” with the last version. As explained by Chan, since the same values P(i,j)/W(i,j) were used six times for the vast majority of points; the execution time is divided accordingly now that they are precomputed in the new matrix Q. Moreover, as Jean-françois pointed out, a significant amount of memory is saved by using only the columns located in the right part of the triangle. In this new version, the BASIC program is 402 bytes. For (R,S)=(30,60), it uses 18855 bytes of data (18.4 kB) mainly for the three matrices W, P and Q. I can't resist the pleasure of sharing this new version with you. Do not hesitate to dissect it: 10 DEF FNL(X)=1+CEIL(X/2) @@ User function for easy column limit indice computation 20 DESTROY ALL @ DELAY 0 @ INPUT"[VA]SRC012a R,S=";R,S @ T0=TIME 30 OPTION BASE 1 @ DIM W(R,R),P(R,R) @@ MAT W and P ranging ( 1..R , 1.. R ) 2x7.04 ko @ OPTION BASE 0 @ DIM Q(1+R,FNL(R)) @@ MAT Q ranging ( 0..R+1 , 1..1+R/2 ) 4.12 ko @ P(1,1)=1 @ M=2 40 FOR I=1 TO R @ FOR J=1 TO I @ W(I,J)=3/(3-(J=1)-(I=J)-(I=R)) @@ W(I,J) is either 0 or 1 or 1.5 or 3 @ NEXT J @ NEXT I 50 FOR K=1 TO S @ FOR I=1 TO M @ FOR J=1 TO FNL(I) @ Q(I,J)=P(I,j)*W(I,J) @@ no division, spare time, preserve precision @ NEXT J @ NEXT I 60 FOR I=1 TO M @ FOR J=1 TO CEIL(I/2) 70 P(I,J)=Q(I-1,J-1)+Q(I-1,J)+Q(I,J-1)+Q(I,J+1)+Q(1+I,J)+Q(1+I,1+J) @@ no neighbors hunting, all I±1 or J±1 in Q’s subscripts range @ P(I,1+I-J)=RES @@ RES is previous computed arithmetic sum stored in P(I,J) 80 NEXT J @ NEXT I @ M=M+(M<R) @@ slow but faster than the IF THEN statment which need an extra line! @ DISP K;TIME-T0 @ NEXT K 90 T=0 @ FOR J=1 TO R @ T=T+P(R,J) @ NEXT J @ DISP TIME-J;R;S;T/6^S @ BEEP |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)