[VA] SRC #012a - Then and Now: Probability
|
10-11-2022, 06:51 PM
(This post was last modified: 10-13-2022 07:55 AM by J-F Garnier.)
Post: #22
|
|||
|
|||
RE: [VA] SRC #012a - Then and Now: Probability
I just finished to debug my code.
Valentin's hint to check that the sum of the probability of all cells should be 1 was very helpful to discover several coding errors :-) The principle of my code is, I believe, similar to Fernando's solution, but the triangle cells are computed in a different order, row by row. Also I took advantage of the symmetry of the starting condition. Note that I used the fact that the HP BASIC doesn't enter a FOR loop if the final index is smaller than the starting index. This is not true in all BASIC, if needed enable back lines 160 and 220. Note also that I used some HP-71 Math ROM matrix statements (and for the fun, one statement of the Math 2B version :-) My code is longer, partially because I avoided multi-statement lines for clarity. The result is very slightly different: 9.51234350205E-6 [updated, was 9.51234350213E-6 in the previous version] Here is my code [updated: lines 90, 140 and 240 replaced by lines 91, 141 and 241]: 10 OPTION BASE 1 20 N=30 ! rows 30 S=60 ! steps 40 DIM A(N,N),B(N,N) 50 ! set up A 60 MAT A=ZER @ MAT B=ZER 70 A(1,1)=1 80 FOR X=1 TO S 90 ! B(1,1)=(A(2,1)+A(2,2))/4 ! row 1 91 B(1,1)=A(2,1)/4+A(2,2)/4 ! row 1 100 B(2,1)=A(1,1)/2+A(2,2)/4+A(3,1)/4+A(3,2)/6 120 B(2,2)=B(2,1) ! row 2 130 B(3,1)=A(2,1)/4+A(3,2)/6+A(4,1)/4+A(4,2)/6 140 ! B(3,2)=(A(2,1)+A(2,2)+A(3,1)+A(3,3))/4+(A(4,2)+A(4,3))/6 141 B(3,2)=A(2,1)/4+A(2,2)/4+A(3,1)/4+A(3,3)/4+A(4,2)/6+A(4,3)/6 150 B(3,3)=B(3,1) ! row 3 160 ! IF N<6 THEN 310 170 ! rows 4..N-2 180 FOR I=4 TO N-2 190 B(I,1)=A(I-1,1)/4+A(I,2)/6+A(I+1,1)/4+A(I+1,2)/6 210 B(I,2)=A(I-1,1)/4+A(I-1,2)/6+A(I,1)/4+A(I,3)/6+A(I+1,2)/6+A(I+1,3)/6 220 ! IF I<5 THEN 270 230 FOR J=3 TO INT((I+1)/2) 240 ! B(I,J)=(A(I-1,J-1)+A(I-1,J)+A(I,J-1)+A(I,J+1)+A(I+1,J)+A(I+1,J+1))/6 241 B(I,J)=A(I-1,J-1)/6+A(I-1,J)/6+A(I,J-1)/6+A(I,J+1)/6+A(I+1,J)/6+A(I+1,J+1)/6 250 B(I,I+1-J)=B(I,J) 260 NEXT J 270 B(I,I-1)=B(I,2) 280 B(I,I)=B(I,1) 290 NEXT I 310 ! row N-1 320 B(N-1,1)=A(N-2,1)/4+A(N-1,2)/6+A(N,1)/2+A(N,2)/4 330 B(N-1,2)=A(N-2,1)/4+A(N-2,2)/6+A(N-1,1)/4+A(N-1,3)/6+A(N,2)/4+A(N,3)/4 340 FOR J=3 TO INT(N/2) 350 B(N-1,J)=A(N-2,J-1)/6+A(N-2,J)/6+A(N-1,J-1)/6+A(N-1,J+1)/6+A(N,J)/4+A(N,J+1)/4 360 B(N-1,N-J)=B(N-1,J) 370 NEXT J 380 B(N-1,N-1)=B(N-1,1) 390 B(N-1,N-2)=B(N-1,2) 400 ! row N 410 B(N,1)=A(N-1,1)/4+A(N,2)/4 420 B(N,2)=A(N,1)/2+A(N,3)/4+A(N-1,1)/4+A(N-1,2)/6 430 FOR J=3 TO INT((N+1)/2) 440 B(N,J)=A(N,J-1)/4+A(N,J+1)/4+A(N-1,J-1)/6+A(N-1,J)/6 450 B(N,N+1-J)=B(N,J) 460 NEXT J 470 B(N,N-1)=B(N,2) 480 B(N,N)=B(N,1) 490 ! 500 MAT A=B 550 NEXT X 555 ! output result 560 DIM C(N) 570 MAT C=RSUM(A) 580 DISP "PROB.=";C(N) >RUN PROB.= 9.512343502105-6 Now, I would be curious to see RPN/RPL solutions :-) J-F |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 14 Guest(s)