Thursday, October 6, 2011

RPL Programming Tutorial - Part 4 - HP 49g+/50g: For-Next

What's it all FOR?

OK, corny titles aside, welcome to Part 4 of the RPL Tutorial for the HP 49g+ and 50g calculators. Part 4 will introduce another fundamental structure in mathematical programming: the FOR-NEXT structure.

The FOR-NEXT structure allows the programmer to designate a set of commands to be repeated a set number of times. The structure can use a counter, known as a dummy variable. The counter can also be part of a calculation.

In general, the FOR-NEXT structure looks something like this:

FOR counter-variable = start-number TO end-number
Instructions go here
NEXT (or END)

Duplicating Pascal's Triangle


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


This is the first six rows of the famous Pascal's Triangle. Pascal's Triangle is used for many purposes. One use of the triangle is to determine the coefficients of the binomial expansion of (x + y)^n, where n is an integer. The top row of Pascal's Triangle is designated as "Row 0".

Any entry in Pascal's Triangle can be found using the Combination function:

COMB(r, n) = r! / (n! * (r - n)!)

Where:
r = the row number
n = the entry number

This program will return all of the entries of a given row.

The Program PASCAL

Comments will be italicized, starting with an asterisk. This program starts with the desired row number R on Level 1 of the Stack.

[RS] [ + ] (<< >>)
[RS] [ 0 ] (&rarr) [ALPHA] [ √ ] (R)

* Store the desired row number in local variable R
[RS] [ + ] (<< >>)
[LS] [ + ] ( { } )

* Puts an empty list on the stack, all the entries will go in this list
[ &rarr ] [SPC]
0 [SPC] [ALPHA] [ √ ] (R)

* Start-number = 0, End-number = R
[LS] [EVAL] (PRG)
[F3] (BRCH)
[LS] [F4] (FOR)

* Inserts the FOR-NEXT structure
[ALPHA] [STO] (K)
* Let K be the counter-variable (or dummy variable)
[SPC] [ALPHA] [ √ ] (R) [SPC] [ALPHA] [STO] (K)
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F1] (COMB)

* Calculates COMB(R, K)
[ + ]
* Adds result to the list
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [SYMB] (P) [F1] (A) [SIN] (S) [F3] (C) [F1] (A) [NXT] (L) [ENTER] [STO>]

The completed program:
<< &rarr R << { } 0 R FOR K R K COMB + NEXT >> >>

Instructions:
1. Enter the desired row number on Level 1 of the Stack.
2. Run PASCAL
3. If the list exceeds the width of the screen, you can press either [HIST] [F2] (VIEW) or [ &uarr ] [F2] (VIEW) to get the list in View Mode. Use the arrow keys ( [ &rarr ] and [ &larr ] ) to scroll the list's contents.

Test:
3rd Row: {1 3 3 1}

6th Row: {1 6 15 20 15 6 1}

12th Row: {1 12 66 220 495 792 924 792 495 220 66 12 1}

This is just one example of using the FOR-NEXT structure. In Part 5, coming soon, we get the calculator to ask for a number. See you then!

Eddie


This tutorial is property of Edward Shore. Mass reproduction or distribution requires express permission of the author.

No comments:

Post a Comment

TI-84 Plus CE: Version 5.3 Highlights

TI-84 Plus CE:  Version 5.3 Highlights Piecewise Function The piecewise( function is added to the Math menu under the math submenu...