HP Forums
(12C) Stirling's approximation - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Stirling's approximation (/thread-9355.html)



(12C) Stirling's approximation - Gamo - 10-24-2017 05:35 AM

For when you need answer from Decimal Factorial with your HP 12C

From Wikipedia:
In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good-quality approximation, leading to accurate results even for small values of n.

Program:
Code:

571
ENTER
2488320
/
CHS
STO 1
139
ENTER
51840
/
CHS
STO 2
288
1/x
STO 3
12
1/x
STO 4
CLx
R/S
ENTER
STO 5
2
x
355
ENTER
113
/
x
Sqr
RCL 5
RCL 5
2
/
Y^x
STO 6
x
RCL 5
e^x
1/x
x
RCL 6
x
STO 7
RCL 5
1/x
ENTER
ENTER
ENTER
RCL 1
x
RCL 2
+
x
RCL 3
+
x
RCL 4
+
x
1
+
RCL 7
x
GTO 37

Example: Clear Register f [REG]

[R/S] result 0 (Initialize)

2.34! ------> 2.34 [R/S] result 2.7976......
4.32! ------> 4.32 [R/S] result 39.2945....
5.43! ------> 5.43 [R/S] result 254.0337...


Gamo


RE: (12C) Stirling's approximation - Gamo - 11-04-2017 05:26 AM

N! approximation using Forsyth's formula with shorter program steps.

Code:

STO 1
ENTER
X
RCL 1
+
6
1/x
+
√x
1
e^x
/
RCL 1
0.5
+
Y^X
2.5066283
x

Example: input N [R/S]

2.34! ------> 2.34 [R/S] result 2.7971......
4.32! ------> 4.32 [R/S] result 39.2928....
5.43! ------> 5.43 [R/S] result 254.0266...

Gamo


RE: (12C) Stirling's approximation - Dieter - 11-04-2017 02:01 PM

(11-04-2017 05:26 AM)Gamo Wrote:  N! approximation using Forsyth's formula with shorter program steps.

Instead of 2 [y^x] you should use [ENTER] [x] which is much faster (and sometimes even more accurate). And why don't you simply use 6 [1/x] instead of 0,1667 ?-) Finally, once again the ENTER is not required and should be omitted.

This leads to the following version, here with a few more digits in sqrt(2pi) and without any registers:

Code:
ENTER
ENTER
x
LastX
+
6
1/x
+
√x
1
e^x
/
X<>Y
0.5
+
Y^X
2.5066283
x
GTO 00

This gets even a tiiiny bit closer to the true results:

2.34 [R/S] => 2.7971...
4.32 [R/S] => 39.2931...
5.43 [R/S] => 254.0287...

And it accurately overflows for x > 69,95757445

Dieter


RE: (12C) Stirling's approximation - Gamo - 11-05-2017 07:39 AM

Dieter Thank You

Totally forgot about 1/6 by using [ 1/x ]
And your program is really improve the accuracy.

Gamo


RE: (12C) Stirling's approximation - Dieter - 11-06-2017 01:41 PM

(11-05-2017 07:39 AM)Gamo Wrote:  Totally forgot about 1/6 by using [ 1/x ]
And your program is really improve the accuracy.

To be honest, the change in accuracy is negligible. The essential message was: please, do not use 2 [y^x] for squaring. [ENTER] [x] is much faster, sometimes more accurate and it even sets LastX correctly.

Dieter