## 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.

### Numworks: Allowing Repeated Calculations in Python

Numworks: Allowing Repeated Calculations in Python Introduction Say we want the user to repeat a calculation or a routine for as lo...