# HP Forums

Full Version: (DM 42) Bernoulli and tangent numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
B2n, Tn: Fast calculation of Bernoulli numbers based on Tangent numbers

Input: n
output: result in register 0 to n, even Bernoulli numbers b2n
note b1= +/-0.5, all other odd numbers=0. For b1 the latest definition is +0.5
can also provide Tangent numbers depending on flag 00

flag 00 cancelled: Calculate even Bernoulli numbers B2n
flag 00 set : calculate tangent numbers Tn
flag is reset after each run

runs in standard 4 stack mode, uses registers 0-n and flag 00

Calculates Bn60 (n=0..30) in less than a second on batteries.
On USB power, calculates up to n=999, i.e. the 0-1998th Bernoulli numbers in about 5min 40 seconds.
Ref
"Fast computation of Bernoulli, Tangent and Secant Numbers. "
Richard P. Brent and David Harvey
6 Algorithms based three-term recurrences
arXiv:1108.0286v3 [math.CO] 5 Sep 2011

The algorithm is stable, and should return Bernoulli numbers accurate to 31-32 digits
2022-03-20 DM-42 version Gjermund Skailand

00 { 126-Byte Prgm }
01▸LBL "Tn"
02 SF 00
03▸LBL "B2n"
04 1ᴇ3
05 ÷
06 2
07 +
08 ENTER
09 ENTER
10 1
11 STO 01
12 1
13▸LBL 00
14 ×
15 STO IND ST Y
16 RCL ST Y
17 IP
18 ISG ST Z
19 GTO 00
20 R^
21▸LBL 01
22 ENTER
23 ENTER
24 ENTER
25 1
26 -
27 R↓
28 RCL IND ST T
29▸LBL 02
30 RCL ST Y
31 RCL- ST T
32 ×
33 2
34 RCL+ ST L
35 RCL× IND ST Z
36 +
37 STO IND ST Y
38 ISG ST Y
39 GTO 02
40 R↓
41 R↓
42 ISG ST X
43 GTO 01
44 FP
45 1
46 STO 00
47 +
48 FS?C 00
49 RTN
50▸LBL 03
51 ENTER
52 IP
53 RCL× IND ST X
54 2
55 LASTX
56 STO+ ST X
57 DSE ST X
58 Y^X
59 ÷
60 LASTX
61 STO+ ST X
62 1
63 -
64 ÷
65 FS? 00
66 +/-
67 FC?C 00
68 SF 00
69 STO IND ST Y
70 R↓
71 ISG ST X
72 GTO 03
73 CF 00
74 END
The same, but using a matrix to store the numbers, so you can easily go beyond 999 ;-)
Also, I prefer not to use flags.
input 30 XEQ "B2n" runs in 0.57 seconds on batteries.

Code:
```00 { 121-Byte Prgm } 01▸LBL "Tn" 02▸LBL 10 03 1 04 NEWMAT 05 EDIT 06 LASTX 07▸LBL 02 @ Tk    k 08 STOEL 09 ISG ST Y 10 X<>Y 11 RCL× ST Y 12 I+ 13 FC? 76 14 GTO 02 15 2 16▸LBL 03 17 0 18 RCLEL 19 I+ 20 FS? 76 21 GTO 00 22▸LBL 04 @ Tj-1 j-k k 23 X<>Y 24 STO× ST Y 25 2 26 + 27 X<>Y 28 RCLEL 29 RCL× ST Z 30 + 31 STOEL 32 DSE ST Y 33 I+ 34 FC? 76 35 GTO 04 36 R^ 37 1 38 STOIJ 39 + 40 GTO 03 41▸LBL "B2n" 42 XEQ 10 43 EDIT 44 CLX 45▸LBL 05 46 +/- 47 ENTER 48 SIGN 49 STO+ ST X 50 + 51 RCLEL 52 2 53 RCL ST Z 54 ABS 55 Y^X 56 X^2 57 LASTX 58 - 59 ÷ 60 × 61 STOEL 62 R↓ 63 I+ 64 FC? 76 65 GTO 05 66▸LBL 00 67 RCLEL 68 EXITALL 69 END```

Cheers, Werner
Hi Werner,
shorter and nicer (as usual), and you found a nice way to avoid the loop limit of 999.
PS I wasn't happy about the flag use either :-)
br Gjermund
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :