The HP48 Homepage  
 

 
News My Programs Software Programming Articles Links Sitemap Search Feedback    
   
 

Programming in User RPL - Answers to exercises

Chapter 1: First Concepts

    1. << 1 2 + >>
    2. << << 1 2 + >> EVAL >>
    3. 3
    4. 3
    5. 1 and << 2 + >>
    6. 3
  1. << 3 ^ pi 4 * 3 / ->NUM >>
  2. << 32 - 5 * 9 / >>
  3. << 9 * 5 / 32 + >>
  4. << INV SWAP INV + INV >>
  5. << DUP2 * 3 ROLLD + / >>
    If you have LASTARG enabled, there is a shorter version: << * LASTARG + / >>
  6. << 4 ROLL * 3 ROLLD * - >>
  7. << ROT - SQ 3 ROLLD - SQ + v/ >>
  8. << 3 PICK 4 * * NEG OVER SQ + v/ SWAP NEG + SWAP 2 * / >>
  9. The most obvious solution is: << 3 PICK 4 * * NEG OVER SQ + v/ SWAP NEG ROT 2 * DUP 3 ROLLD / 3 ROLLD / DUP2 + 3 ROLLD NEG + >>. But Peter Karp sent two solutions that are shorter: << 2 ->LIST SWAP / EVAL SWAP -2 / DUP SQ ROT - v/ DUP2 + ROT ROT - >> and also << 3 PICK / SWAP ROT -2 * / DUP SQ ROT - v/ DUP2 + ROT ROT - >>.
  10. The most obvious solution is: << 3 DUPN * SWAP ROT * + SWAP ROT * + 2 * >>. However, Joe Horn has a better solution. It is based on factoring of the equation to S = 2(d(h + w) + hw). So, here is the program. Note it uses LASTARG:
    << * LASTARG + ROT * + 2 * >>

Chapter 2: Local Variables

  1. << -> a b u c d v
       << '(d*u-b*v)/(a*d-b*c)' EVAL
          '(a*v-c*u)/(a*d-b*c)' EVAL
       >>
    >>
    
    Of course, you can use RPN notation if you prefer:
    << -> a b u c d v
       << d u * b v * - a d * b c * - /
          a v * c u * - a d * b c * - /
       >>
    >>
    
    1. << -> a b c d e f g h i 'a*(e*i-f*h)+b*(f*g-d*i)+c*(d*h-e*g)' >>
    2. << -> a b c d e f g h i << a e i * f h * - * b f g * d i * - * + c d h * e g * - * + >>
  2. << -> a b c d e f g h i
       << a e i * f h * - * b f g * d i * - * + c d h * e g * - * + INV
          -> det
          <<
             e i * f h * - det *
             b i * c h * - det * NEG
             b f * c e * - det *
             d i * f g * - det * NEG
             a i * c g * - det *
             a f * c d * - det * NEG
             d h * e g * - det *
             a h * b g * - det * NEG
             a e * b d * - det *
          >>
       >>
    >>
    

    This program is extremely inefficient. It uses the formula

    to calculate the inverse matrix. First, it calculates the determinant and its reciprocal, storing it on the local variable det. Yes, you can use local variables inside local variables. Then, it calculates each 2x2 determinat, multiplies by the inverse determinant and if necessary changes the sign.

Chapter 3: Conditional Tests

    1. 0
    2. 1
    3. 0
    4. 1
    1. 0
    2. 0
    3. 1
    4. 0
    1. 1
    2. 0
    3. 1
    4. 0
    5. 0
    6. 1
    7. 0
  1. 0
    1. Command1 and Command5
    2. Command3, Command4 and Command5
    3. 0, 0, and any other value
    4. Command2 and Command5
  2. 2.2222...
  3. << IF DUP FS? THEN
          CF
       ELSE     @ Since the flag is already set, we don't need
          DROP  @ to set it, just drop it's number
       END
    >>
    
  4. << IF THEN          @ If the value is true
          SF            @ Set
       ELSE
          CF            @ Otherwise clear
       END
    >>
    
  5. << -> l m n
       << IF l m > l n > OR THEN
             IF m n < THEN
                l m 'l' STO 'm' STO
             ELSE
                l n 'l' STO 'n' STO
             END
          END
          IF m n > THEN
             m n 'm' STO 'n' STO
          END
          l m n
       >>
    >>
    
  6. << ROT 2 -
       IF DUP 0 < THEN   @ January or February?
          12 +           @ Correct month
          SWAP 1 - SWAP  @ Correct year
       END
       3 ROLLD
       100 / DUP FP 100 *  SWAP IP  @ Split year
       DUP 4 / IP SWAP 2 * - SWAP DUP 4 / IP
       + + + SWAP 2.6 * .2 - IP + 7 MOD  @ Apply formula
       -> dw
       << CASE
             dw 0 == THEN "Sunday" END
             dw 1 == THEN "Monday" END
             dw 2 == THEN "Tuesday" END
             dw 3 == THEN "Wednesday" END
             dw 4 == THEN "Thrusday" END
             dw 5 == THEN "Friday" END
             dw 6 == THEN "Saturday" END
             "Other day?!"
          END
       >>
    >>
    
    This program can be improved. First, we can set Saturday to be the default clause because it'll never be another day. But it can be improved even further, without the CASE. But that removes the purpose of the exercise. Anyway, we can replace -> dw <<...>> by
    { "Sunday"
      "Monday"
      "Tuesday"
      "Wednesday"
      "Thrusday"
      "Friday"
      "Saturday" }
    SWAP 1 + GET
    

Chapter 4: Loop Structures

  1. { 100 81 64 49 36 25 16 9 4 }
  2. << { } 10
       WHILE
          DUP 1 =/
       REPEAT
          DUP 1 - 3 ROLLD SQ + SWAP
       END
       DROP
    >>
    
  3. << 1        @ Temporary factorial (this is the number
                @ that will be multiplyied)
       2        @ Count
       -> n f i
       << WHILE
             i n <=   @ Repeat until count has reached the
                        @ desired number
          REPEAT
             'f' i STO*
             'i' 1 STO+
          END
          f     @ Output the fatorial
       >>
    >>
    
    or, using DO:
    << 1 2 -> n f i
       << DO
             'f' i STO*
             'i' 1 STO+
          UNTIL
             i n >
          END
          f
       >>
    >>
    
  4. << 0         @ Number of years elapsed
       3E7 2E8   @ Initial populations
       -> ny pa pb
       << DO
             'pa' 1.03 STO*
             'pb' 1.015 STO*
             'ny' 1 STO+   @ Increase population & number of years
          UNTIL
             pa pb >=
          END
          pa "Pop. A" ->TAG
          pb "Pop. B" ->TAG
          ny "No. years" ->TAG
       >>
    >>
    
  5. << 0     @ Time elapsed
       -> m t
       << WHILE
             m .5 >=
          REPEAT
             'm' 2 STO/     @ Decrement mass
             't' 50 STO+    @ Increment time
          END
          m 1_g ->UNIT "Mass" ->TAG
          @ Cute display for mass.
          t  @ We now have the time. Let's convert it to
             @ hours minutes and seconds.
          3600 / IP "h " +  @ Hours
          t 3600 MOD DUP 't' STO  @ Seconds - hours
          60 / IP "min " + +  @ Minutes
          t 60 MOD "s" + +  @ Finally
       >>
    >>
    
    1. << 1     @ First Item
         2 50 FOR i
            i 2 * 1 - i / +   @ Calculate term & add
         NEXT
      >>
      
    2. << .04   @ First Item
         2 50 FOR j
            2 j ^ 51 j - / +
         NEXT
      >>
      
    3. << 1406  @ First Item
         36 1 FOR k
            k k 1 + * 38 k - / +
         -1 STEP
      >>
      
      or, you could change it to calculate in reverse order, so that it isn't necessary to use -1 STEP.
    4. << 1
         2 10 FOR i
            i SQ i /    @ Absolute value
            -1 i ^ * -  @ See sign, & add
         NEXT
      >>
      
    5. << 1000
         2 50 FOR a
            1000 a 1 - 3 * - a /
            -1 k ^* -
         NEXT
      >>
      
    6. << 48
         1 10 FOR n
            480 n 5 * - n 10 + /
            -1 n ^ * +
         NEXT
      >>
      




  6. << 1
       3 105 FOR k
          k 3 ^ -1 k 2 / CEIL ^ * INV -
       2 STEP
       32 * 3 XROOT
    >>
    
  7. << 4 3 -> d
       << DO
             4 d / -1 d 2 / CEIL ^ * - 2 'd' STO+
          UNTIL
             DUP pi ->NUM - ABS .01 <
          END
       >>
    >>
    
  8. << 1 -> x d
       << 1 WHILE
             DUP x EXP - ABS 1E-11 >
          REPEAT
             x d ^ d ! / +
             'd' 1 STO+
          END
       >>
    >>
    
    1. << 1 2 ROT
         FOR n
            n INV +
         NEXT
      >>
      
    2. << DUP -> x
         << 1 20 FOR i
               x i 2 * ^ i 2 * 1 + ! /
               -1 i ^ * +
            NEXT
         >>
      >>
      
    3. << DUP INV SWAP -> x
         << 2 x FOR n
               n x n 1 - - / +
            NEXT
         >>
      >>
      
  9. << DUP 2 / SWAP -> y
       << 1 20 START
             DUP SQ y + SWAP 2 * /
          NEXT
       >>
    >>
    
  10. << DUP DUP v/ SWAP 2 / 3 ROLLD -> y r
       << DO
             DUP SQ y + SWAP 2 * /
          UNTIL
             DUP r - ABS 1E-11 <
          END
       >>
    >>
    
  11. << ROT DUP DUP STEQ  @ Save a copy of equation in EQ
       EQ-> -            @ Split the two sides of the equation
                         @ and subtract them, so we get a
                         @ equation with a side equal to 0
       DROP ROT DUP2 DUP PURGE .d    @ Calculate derivate
       -> f v d    @ Save function, variable and derivate
       << 1 30 START
             DUP v STO f EVAL d EVAL / -   @ Calculate approximation
          NEXT
       >>
    >>
    
  12. << 4 ROLL DUP DUP STEQ OBJ-> SWAP DROP
       EQ-> -
       DROP 4 ROLL DUP2 DUP PURGE .d -> e f v d
       << DO
             DUP v STO
             f EVAL DUP d EVAL / ROT SWAP -
          UNTIL
             SWAP ABS e <
          END
       >>
    >>
    

Chapter 6: Getting input

  1. << "Enter A:" "" INPUT OBJ->
       "Enter B:" "" INPUT OBJ->
       "Enter C:" "" INPUT OBJ->
       3 PICK 4 * * NEG OVER SQ + v/
       SWAP NEG ROT 2 * DUP 3 ROLLD / 3 ROLLD / DUP2 + 3 ROLLD NEG *
    >>
     
  2. << "QUADRATIC EQUATION"
       { { "A:" "AX^2+BX+C=0" 0 1 9 13 }
         { "B:" "AX^2+BX+C=0" 0 1 9 13 }
         { "C:" "AX^2+BX+C=0" 0 1 9 13 } }
       { } DUP DUP
       IF INFORM THEN
          OBJ-> DROP 3 PICK 4 * * NEG OVER SQ + v/
          SWAP NEG ROT 2 * DUP 3 ROLLD / 3 ROLLD / DUP2 + 3 ROLLD NEG *
          2 ->LIST
          "SOLUTION"
          { "X':" "X'':" }
          { } DUP
          5 ROLL
          IF INFORM THEN DROP END
       END
    >>
    
  3. << "SOLVE SYSTEM"
       { { "A:" "AX+BY=U" 0 1 9 13 }
         { "B:" "AX+BY=U" 0 1 9 13 }
         { "U:" "AX+BY=U" 0 1 9 13 }
         { "C:" "CX+DY=V" 0 1 9 13 }
         { "D:" "CX+DY=V" 0 1 9 13 }
         { "V:" "CX+DY=V" 0 1 9 13 } }
       3 { } DUP
       IF INFORM THEN
          -> a b u c d v
          << '(d*u-b*v)/(a*d-b*c)' EVAL
             '(a*v-c*u)/(a*d-b*c)' EVAL
          >>
       END
    >>
    
  4. << "INVERSE MATRIX" DUP
       { { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 }
         { "" "" 0 1 9 13 } } DUP 3 ROLLD
       { 3 1 } DUP 4 ROLLD
       { 0 0 0 0 0 0 0 0 0 } DUP DUP 6 ROLLD
       IF INFORM THEN
          OBJ-> DROP
          -> a b c d e f g h i
          << a e i * f h * - * b f g *
             d i * - * + c d h * e g * - * + INV
             -> det
             <<
                e i * f h * - det *
                b i * c h * - det * NEG
                b f * c e * - det *
                d i * f g * - det * NEG
                a i * c g * - det *
                a f * c d * - det * NEG
                d h * e g * - det *
                a h * b g * - det * NEG
                a e * b d * - det *
             >>
          >>
          9 ->LIST
          IF INFORM THEN DROP END
       END
    >>
    
 
 

This page was created by Eduardo M Kalinowski