Post Reply 
Bernoulli Num,ber using Series Approximation
12-18-2013, 05:57 AM (This post was last modified: 01-17-2014 05:24 AM by Namir.)
Post: #1
Bernoulli Num,ber using Series Approximation
Bernoulli Numbers using Series Approximations

Algorithm

Given integer n and tolerance toler

Code:
IF n=1 THEN Return -0.5
IF n Mod 2 = 1 THEN Return 0 

IF n=1 THEN
  RETURN -0.5 
ELSE
  sum=1 
  i=2 
  term=2*toler 
  WHILE term>toler DO
    term=1/i^n 
    sum=sum+term 
    i=i+1 
  END 
  IF n MOD 4 == 0 THEN chs=-1 ELSE chs=1
  t=chs*(n!)/pi^n/2^(n-1) 
  Return t*sum 
END


HP-41C Implementation

Memory Map

R0 = n
R1 = toler
R2 = i
R3 = Sum
R4 = term
R6 = used in LBL 0

Implementation

Code:
1 LBL A            # toler Enter n and press [A]
2 STO 0
3 X<>Y
4 STO 1
5 1
6 RCL 0
7 X=Y?
8 GTO 8
9 2
10 GSB 0            # MOD
11 1
12 X=Y?
13 GTO 9
14 1
15 STO 3      # sum = 1
16 STO 2      # i = 1
17 LBL 1      # Start summation loop
18 1
19 STO+ 2     # i=i+1
20 RCL 2
21 RCL 0
22 Y^X       
23 1/X         # Calculate 1/i^n
24 STO+ 3      # sum = sum + 1/i^n
25 RCL 0
26 X<=Y?       # toler <= term?
27 GTO 1       # resume to the next iteration
28 CF 0        # start calculating other factor
29 RCL 0
30 4
31 GSB 0
32 X=0?
33 SF 0
34 RCL 0
35 N!            # n!
36 PI
37 RCL 0
38 Y^X         # pi^n
39 /
40 2
41 RCL 0
42 1
43 -
44 Y^X          # 2^(n-1)
45 /
46 RCL 03
47 *
48 F? 0             # change sign?
49 CHS
50 CF 0        
51 RTN
52 LBL 0                 # Implement MOD operator
53 STO 6
54 X<>Y
55 STO 7
56 X<>Y
57 /
58 INT
59 RCL 6
60 *
61 RCL 7
62 X<>Y
63 -
64 RTN
65 LBL 9
66 0
67 RTN
68 LBL 8
69 0
70 .
71 5
72 CHS
73 RTN
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)