hp41programs

dice

A Dice Problem for the HP-41


Overview
 

-If you throw 2 identical dices having 6 sides ( numbered from 1 to 6 ) , there are 5 combinations giving the total 6:

    1 + 5 = 2 + 4 = 3 + 3 = 4 + 2 = 5 + 1.

-The following program calculates how many dice combinations D( n , s , t ) result in a given total t
  when you throw n dices ( n > 1 ) having s sides ( numbered 1 , 2 , ...... , s )
 

Program Listing
 

Data Registers:               All registers are cleared by this program ( line 02 ), but only R00 thru Rnn are actually used
Flags: /
Subroutines: /
 

-As usual, synthetic registers M , N , O , P can be replaced by any unused standard registers ( Rmm  with m > n ).
 
 

01  LBL "DST"
02  CLRG
03  X<>Y
04  STO N
05  X<> Z
06  STO M    
07  -
08  .1
09  %
10  STO O       
11  LBL 01
12  RCL M    
13  STO P
14  DSE P
15  RCL O
16  INT
17  RCL N       
18  /
19  FRC
20  X#0?
21  GTO 02
22  LASTX
23  ENTER^
24  R^
25  -
26  RCL 00      
27  ST* Y
28  -
29  X<>Y
30  X#0?
31  /
32  X=0?
33  SIGN
34  STO 00      
35  ST+ IND M
36  LBL 02
37  RCL P
38  SIGN
39  ST+ L
40  RCL IND L
41  ST+ IND P
42  DSE P
43  GTO 02
44  ISG O
45  GTO 01
46  RCL 01      
47  CLA
48  END

 
    ( 77 bytes / SIZE n+1 )
 
 

         STACK            INPUTS           OUTPUTS
              Z                 n                 /
              Y                 s                 /
              X                 t         D( n , s , t )

 
Example:     How many combinations result in a total of 41 if you throw 7 dices having 12 sides ( numbered from 1 to 12 ) ?

     7   ENTER^
    12  ENTER^
    41  XEQ "DST"   yields   1,369,368   in X-register and in R01   ( execution time = 67 seconds )

Notes:

1- This routine requires:   n < =  t   < =  n.s  ( otherwise the answer is of course 0 but "DST" may give a wrong result )
2- The propertie    D( n , s , t )  =  D( n , s , n.s + n - t )  could be used to reduce execution time when   n.s + n - t  <  t
     ( execution time is approximately proportional to n.t )
3- If you want to know the probability rather than the number of combinations,
           add     RCL N     RCL M     Y^X     /       after line 46.
4- This program doesn't work if   t - n > 999 . If you want to overcome this limitation:

      replace line     44        by   DSE O
      replace lines 15-16     by   RCL Q    RCL O   -
      replace lines 08 to 10  by   1  +   STO O   STO Q
 

Reference:

-Issues # 29 and # 41 of  "48SXTANT"  ( a Dutch magazine edited by Robert Pulluard ) ( in French ).