Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

Saturday, December 10, 2022

HP 20S: Gamma Function Approximation (Stirling's Formula)

HP 20S:  Gamma Function Approximation (Stirling's Formula)



Introduction


The gamma function uses the approximation sequence:


Let t = x + 9


Then calculate:


Let G = Γ(t) ≈ 

exp( ln √(2 × π ÷ t) + t × ln t - t + (12 × t)^-1 - (360 × t^3)^-1 + (1260 × t^5)^-1 )


Note:

(360 × t^3)^-1  = (12 × t)^-1 × (30 × t^2)^-1

(1260 × t^5)^-1 = (360 × t^3)^-1 × (3.5 × t^2)^-1


While x > t:  

G = G ÷ x

x = x + 1

End Loop


Display G as the final answer 


The approximation polynomial is used for higher values because for the approximation is more accurate for higher values.  



HP 20S:  Gamma Approximation

(63 steps)


Key Code:  { Key }


61, 41, b :  { LBL B }

21, 1 :  { STO 1 }

75 :   { + }

9 :  { 9 }

74 : { = }

21, 2 : { STO 2 }

32 : { +/- }

21, 0 : { STO 0 }

32 : { +/- } 

55 : { × }

13 : { LN }

74 : { = }

21, 75, 0 : { STO+ 0 }

2 :  { 2 }

55 : { × }

61, 22 : { π }

45 : { ÷ }

22, 2 : { RCL 2 }

74 : { = }

11 : { √ }

13 : { LN }

21, 75, 0 : { STO+ 0 }

1 : { 1 }

2 : { 2 }

55 : { × }

22, 2 : { RCL 2 }

74 : { = }

15 : { 1/x }

21, 75, 0 : { STO+ 0 }

45 : { ÷ }

3 : { 3 }

0 : { 0 }

45 : { ÷ }

22, 2 : { RCL 2 }

51, 11 :  { x^2 }

74 : { = }

21, 65, 0 : { STO- 0 }

45 : { ÷ }

3 : { 3 }

73 : { . }

5 : { 5 }

45 : { ÷ }

22, 2 : { RCL 2 }

51, 11 : { x^2 }

74 : { = }

21, 75, 0 : { STO+ 0 }

22, 0 : { RCL 0 }

12 : { e^x }

21, 0 : { STO 0 }

61, 41, 0 : { LBL 0 }

22, 1 : { RCL 1 }

21, 45, 0 : { STO÷ 0 }

1 : { 1 }

21, 75, 1 : { STO+ 1 }

22, 2 : { RCL 2 }

31 : { INPUT }

22, 1 : { RCL 1 }

61, 42 : { x≤y? }

51, 41, 1 : { GTO 1 }

51, 41, 0 : { GTO 0 }

61, 41, 1 : { LBL 1 }

22, 0 : { RCL 0 }

61, 26 : { RTN }



Examples


Γ(0.5) returns 1.77245385109


Γ(4.4) returns 10.1361018514


Calculate the gamma function, press [ XEQ ] B. 


This program is based on the approximation code of the HP 25.  



Source:


Davidson, Jim, and John Vlissides.  "HP-25 Program-Gamma Function"  ENTER: 65 NOTES  Vol. 3 No. 10  December 1976



All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, December 15, 2018

TI-60: Triangle Numbers

TI-60:  Triangle Numbers

Introduction

The following program calculates T_n, the nth triangle number.  Triangle numbers are determined by the sum:

T_n = ∑k, from k = 0 to n

The TI-60 does not have either comparison commands or a goto command.  Luckily for the TI-60 (not the case for the TI-55 III), the RST sends the program pointer back to the first step (Step 00) and continues to execute.

We simulate a loop by counting down, when the counter reaches -1, the square root function will cause an error, forcing execution to stop.  You'll see this trick towards the end of the program.

Instructions:

1.  In RUN mode (outside of LRN), store the following values:

R0 = 0 
R1 = n

2.  Execute the program by pressing [RST], [R/S].  The program is done when you see "Error".

3.  Clear the error by pressing [CE/C].

4.  Recall R0.  This is your triangle number.  (R1 will have -1).

TI-60 Program: Triangle Numbers

PC00  OP71:  RCL 
PC01  OP01:  1
PC02  OP61:  STO 
PC03  OP85:  +
PC04  OP00:  0     // sum R1 in R0
PC05  OP75:  -
PC06  OP01:  1
PC07  OP95:  =
PC08  OP61:  STO
PC09  OP01:  1    // store R1 -1 in R1
PC10  OP86:  √  // square root of R1, if less than 0, program stops<0 ending="" error="" font="" loop="" occurs="" the="">
PC11  OP22:  RST  // reset back to step 00

Examples:

T_5 = 15  (0 [STO] 0,  5 [ST0] 1, [RST] [R/S], [CE/C]  [RCL] 0)

T_35 = 630  (in about 45 seconds)

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Tuesday, October 18, 2011

RPL Programming Tutorial - Part 12 - HP 49g+/50g: Subroutines and the START Loop

Subroutines

Welcome to Part 12 of the RPL Tutorial. In Part 12, we will cover subroutines. A subroutine is a named routine defined in the program, which is saved for later use. the subroutine is called whenever the need arises.

One way to approach subroutines is to type and store the subroutines at the beginning at the program. Unless the user desires to store the subroutine permanently, the subroutine must be purged at the end of the program. Here is one template for a program using a subroutine:

<< << subroutine >> 'SUB1' STO
main program
'SUB1' PURGE >>


Using two or more subroutines:

<< < subroutine1 >> 'SUB1' STO
<< subroutine2 >> 'SUB2' STO
...
main program
...
{'SUB1' 'SUB2' ... } PURGE >>


START-NEXT Structure

The START-NEXT structure is like the FOR-NEXT structure, except that the START-NEXT structure does not use a counter. A structure looks like this:

<< start stop START commands NEXT >>

Keystroke sequence: [LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

The program presented in Part 12 will illustrate using a subroutine and a START-NEXT structure.

Building a list of random numbers

In this program, we are going to pick d integers from low (l) to high (h) - allowing the choices to repeat.

The program uses the subroutine named SUB1:

<< H L - RAND x IP L + >>

SUB1 will be called up in the START-NEXT loop.

The other variables used in the program are:

L = low number,
H = high number, and
D = # of draws

To build the list, the stack is cleared, allowing the program to fill it with draws. Once the draws are complete, DEPTH →LIST will be used to create the list.

The Program SAMP

[RS] [ + ] ( << >> )
[RS] [ + ] ( << >> )
[ALPHA] [MODE] (H) [SPC] [ALPHA] [NXT] (L) [ - ]
[LS] [SYMB] (MTH) [NXT] [F1] (PROB) [F4] (RAND) [ x ]
[LS] [SYMB] (MTH) [F5] (REAL) [NXT] [F5] (IP) [ALPHA] [NXT] (L) [ + ] [ ↓ ]
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[STO>]

* Enters are stores the subroutine SUB1
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [NXT] (L) [ ' ] (O) [+/-] (W) [RS] 3 (?)
* [ALPHA] [RS] 3 sequence gives the question mark character, the ? is not marked on the keyboard
[ALPHA] [ → ] [LS] [EVAL] (PRG) [NXT] [F5] (IN) [NXT] [F1] (PROMP) [ ' ] [ALPHA] [NXT] (L) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [MODE] (H) [TOOL] (I) [APPS] (G) [MODE] (H) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [MODE] (H) [ → ] [STO>]
[RS] [ x ] ( " " ) [ALPHA] [ALPHA] [LS] 3 (#) [SPC] [F4] (D) [ √ ] (R) [F1] (A) [+/-] (W) [SIN] (S) [RS] 3 (?) [ALPHA] [ → ] [F1] (PROMP) [ ' ] [ALPHA] [F4] (D) [ → ] [STO>] [RS] [backspace] (CLEAR)

* Enters the prompts for H, L, and D.
1 [SPC] [ALPHA] [F4] (D)
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F3] (START)

* Inserts the START-NEXT structure
[ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA]
* Calls the subroutine SUB1
[ ↓ ]
[LS] [EVAL] (PRG) [F1] (STACK) [NXT] [F6] (DEPTH)
[LS] [EVAL] (PRG) [F6] (LIST) [F4] ( →LIST )

* Turns the stack into a list with DEPTH →LIST
[LS] [ + ] ( { } )
[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TAN] (U) [F2] (B) 1 [ALPHA] [ → ]
[SPC] [ ' ] [ALPHA] [MODE] (H) [ → ]
[SPC] [ ' ] [ALPHA] [NXT] (L) [ → ]
[SPC] [ ' ] [ALPHA] [F4] (D) [ → ] [ → ] [ → ]
[LS] [EVAL] (PRG) [F2] (MEM) [F1] (PURGE) [ENTER]

* Clean up and entry termination

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [F1] (A) [HIST] (M) [SYMB] (P) [ENTER] [STO>]

The complete program:

<<
<< H L - RAND x IP L + >> 'SUB1' STO
"LOW?" PROMPT 'L' STO
"HIGH?" PROMPT 'H' STO
"# DRAWS?" PROMPT 'D' STO
CLEAR
1 D START SUB1 NEXT
DEPTH →LIST { SUB1 H L D } PURGE >>


Instructions:

1. Run SAMP
2. Enter the low number at the "LOW?" prompt, press [LS] [ON] (CONT)
3. Enter the high number at the "HIGH?" prompt, press [LS] [ON] (CONT)
4. Enter the number of draws at the "# OF DRAWS?" prompt, press [LS] [ON] (CONT)
5. Obtain a list. Use [HIST] [F2] (VIEW) to see the list.

Example:

1. Draw 6 numbers from 100 to 999. A result may be:

{361, 779, 156, 295, 349, 382}

2. Draw 12 2 digit numbers (10 to 99). A result may be:

{52, 63, 83, 76, 52, 54, 71, 47, 70, 16, 23, 41}

This concludes Part 12 of our tutorial. Up next we will make a custom menu and use it to help make programming easier. As always, thank you visiting - Eddie


This tutorial is property of Edward Shore. Mass reproduction and duplication requires express permission of the author.

Tuesday, October 11, 2011

RPL Programming Tutorial - Part 8 - HP 49g+/50g: The WHILE Loop

While We Are Here...

Welcome to Part 8 of the RPL Programming Tutorial. Today, we will cover the WHILE-REPEAT-END programming structure. This structure is similar to the WHILE-WEND programming structure in BASIC. In this loop certain commands are repeated while a condition is true.

The WHILE-REPEAT-END structure looks like this:

WHILE this condition is met
REPEAT these instructions
END


The Program DBY2

This program takes the absolute number from the stack. As long as the number is greater than 1, the program keeps dividing the number by 2.

Note the use of DUP in this program. The DUP (Duplicate) command duplicates the contents of Level 1. It is necessary because tests consume the contents of levels 2 and 1.

[RS] [ + ] (<< >>)
[LS] [ ÷ ]
[LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)

* The DUP command
[LS] [EVAL] (PRG) [F3] (BRCH) [LS] [F6] (WHILE)
* Enters the WHILE-REPEAT-END structure
1 [LS] [1/X] ≥
* Enters the while condition
[ &darr ] 2 [ ÷ ] [LS] [EVAL] (PRG) [F1] (STACK) [F1] (DUP)
* Enters the loop instructions
[EVAL] [ENTER]
* Simplifies the number and terminates program entry

[ ' ] [ALPHA] [ALPHA] [F4] (D) [F2] (B) [1/X] (Y) 2 [ENTER] [STO>]

The Complete Program:

<< ABS DUP
WHILE 1 ≥
REPEAT 2 / DUP
END EVAL >>


Instructions:

1. Enter a number.
2. Run DBY2.

Examples:

10 returns 5/8
96 returns 3/4
-105 returns 105/128

This concludes Part 8 of our tutorial. Next time, we will create custom menus. It's going to be a lot of fun! Until next time, Eddie.


This tutorial is property of Edward Shore. Mass duplication and reproduction requires express permission from the author.

Python: Drawing a Ring of Circles

Python: Drawing a Ring of Circles The goal: Draw a number of circles that revolve around the origin (0,0). The center of each circle i...