Post Reply 
Programming exercise (RPL/RPN) - Reciprocal Fibonacci Constant
02-18-2017, 09:22 AM (This post was last modified: 02-18-2017 09:26 AM by Ángel Martin.)
Post: #27
RE: Programming exercise (RPL/RPN) - Reciprocal Fibonacci Constant
And here's the MCODE for this exercise. It includes four different functions, as follows:

1. FIB, the "straight' Fibonacci number. Enter n in X, result Fn in X and n in Lastx.

2. sFIB, partial sum of Fibonacci numbers. ("s" is the SIGMA char). Straight addition of all Fibonacci numbers up to n.

3. sIFIB, partial sum of the inverse of Fibonacci numbers, the subject of this thread. For n>=46 this is the PSI constant as Gerson explained. The execution time for n=46 is 2.87 seconds on a normal-speed HP-41.

4. FIBI, the "Fibonacci Inverse" Defined as F'n = 1/F'n-2 + 1/F'n-1. Note that this is not the same as the inverse of Fibonacci, which would simply be 1/Fn

This last one is probably of no real interest but the code was "asking for it", if you know what I mean.

Code:

082    "B"    
009    "I"             Sum of inverses of Fibonnaci
006    "F"             PSI constant with n>-46
009    "I"    
04E    "S"    
248    SETF 9            inverse flag
033    JNC +06    
082    "B"    
009    "I"             Sum of Fibonacci
006    "F"    
04E    "S"    
244    CLRF 9            not inverse 
108    SETF 8            summation flag
063    JNC +12d    
089    "I"            Fibonnaci Inverse
002    "B"            (not inverse of Fibinacci)
009    "I"      
006    "F"    
248    SETF 9            inverse flag
02B    JNC +05    
082    "B"              Fibonnaci
009    "I"    
006    "F"    
244    CLRF 9           not inverse    
104    CLRF 8            no sums
0F8    READ 3(X)        x
128    WRIT 4(L)    
2EE    ?C#0 ALL         x=0?
3A0    ?NC RTN          yes, end here.
361    ?NC XQ           (this includes SETDEC)
050    ->14D8           [CHK_NO_S]
05E    C=0 MS           absolute value
088    SETF 5           do integer portion
0ED    ?NC XQ           leaves result in 13-digit form
064    ->193B           [INTFRC]- doesn't need DEC
0E8    WRIT 3(X)        n = int(|x|)
10E    A=C ALL    
04E    C=0  ALL    
268    WRIT 9(Q)        Fn-2
35C    PT=12            C= 1
050    LD@PT- 1    
36E    ?A#C ALL         n=1?
3A0    ?NC RTN          yes, end here.
070    N=C ALL          Fn-1
10C    ?FSET 8    
013    JNC +02          [NOSUM]
168    WRIT 5(M)        initial sum = 1
10E    A=C ALL          ko = 1
04E    C=0  ALL    
35C    PT=12            C= 1
050    LD@PT- 1    
01D    ?NC XQ           k+1
060    ->1807              [AD2_10]
128    WRIT 4(L)        k=k-1
278    READ 9(Q)        Fn-2
10E    A=C ALL    
0B0    C=N ALL          Fn-1
268    WRIT 9(Q)        becomes the next Fn-2
01D    ?NC XQ           Fn = Fn-1 + Fn-1
060    ->1807           [AD2_10]
070    N=C ALL          becomes the next Fn-1
24C    ?FSET 9          inverses?
033    JNC  +06         no, skip
239    ?NC XQ    
060    ->188E           [ONE_BY_X13]
10C    ?FSET 8          summation?
027    JC  +04          yes, skip
070    N=C ALL          becomes the next Fn-1
10C    ?FSET 8          summation?
02B    JNC +05          no, skip
178    READ 5(M)        get partial sum
025    ?NC XQ           add term
060    ->1809           [AD1_10]
168    WRIT 5(M)        update partial sum
138    READ 4(L)        k
10E    A=C ALL    
0F8    READ 3(X)        n
36E    ?A#C ALL    
317    JC  -30d         [LOOP]
0B0    C=N ALL    
10C    ?FSET 8          summation?
013    JNC +02          nope
178    READ 5(M)    
331    ?NC GO           Overflow, DropST, FillXL & Exit
002    ->00CC           [NFRX]

Cheers,
ÁM

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming exercise (RPL/RPN) - Reciprocal Fibonacci Constant - Ángel Martin - 02-18-2017 09:22 AM



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