Post Reply 
[VA] SRC #007 - 2020 April 1st Ramblings
04-16-2020, 04:41 PM
Post: #16
RE: [VA] SRC #007 - 2020 April 1st Ramblings
 
Hi, all:

I see that Paul Dale and Juan14 recently addressed my Rambling 5, which was left unanswered so far, so thanks to both of you for addressing it and consequently I'm giving here my original solution and comments (all code is for the HP-71B + Math ROM):

Quote:5) Finding some simple way to express the Nth Fibonacci number using them, perhaps as simple as a few lines of RPN/RPL code or a short single-line user-defined function, used like this: FNF(1) = 1, FNF(2) = 1, FNF(3) = 2, ..., FNF(10) = 55, ...

My original solution consists of the following code, which simply initializes two numeric variables (one real, one complex) to hold some constants for efficiency's sake, and then implements a single-line user-defined function FNF(N), which takes N (whether integer or not) and uses a single instance of the Hyperbolic Sine (SINH) function (as required) of a complex argument to directly return the N-th Fibonacci number, FN :

      1  DESTROY ALL @ COMPLEX Z @ X=2/SQR(5) @ Z=(LN((1+SQR(5))/2),PI/2)
      2  DEF FNF(N)=REPT(X*SINH(N*Z)/(0,1)^N)

Unlike Juan14's solution, mine doesn't distinguish between odd N and even N, a single SINH suffices for every real N. Also notice that (0,1) is the value i (the imaginary unit) and (1+√5)/2 is φ, the Golden Ratio. Let's check it:

      >RUN      (just once, to initialize the two needed constants)

and now we'll use FNF directly from the command line to compute and display the first 16 Fibonacci numbers F1 .. F16

      >FOR N=1 TO 16 @ DISP USING "2D,2X,3D.8D";N;FNF(N) @ NEXT N

            1      1.00000000
            2      1.00000000
            3      2.00000000
            4      3.00000000
            5      5.00000000
            6      8.00000000
            7     13.00000000
                  ...
           15    610.00000000
           16    987.00000000


which are fully correct. The argument N isn't constrained to be an integer so we can compute F20.20 like this:

      >FNF(20.20)

            7448.4401


and we can check the basic recurrence FN + FN+1 = FN+2 for N = 20.20 like this:

      >FNF(20.20)+FNF(21.20),FNF(22.20)

            19500.2694   19500.2694


Just for fun, let's compute FPi and Fe :

      >STD @ FNF(PI),FNF(EXP(1))

            (FPi =) 2.11702705791   (Fe =) 1.7308152698


(notice that when rounded to 4 digits we coincidentally have Fe = 1.731 while √3 = 1.732 and thus we have Fe ~ √3 )

Last, and also for fun, let's compute N such that FN is a given value, i.e: inverse Fibonacci. Add this 3rd program line:

      3  INPUT F @ FNROOT(1,100,FNF(FVAR)-F) @ GOTO 3


and let's try some values:

      >FIX 8
      >RUN
           ? 55
                     10.00000000    (F10 = 55)
           ? 6765
                     20.00000000    (F20 = 6765)
           ? PI
                     4.09041655     (F4.09041655 = Pi)
           ? 2020
                     17.48828958    (F17.48828958 = 2020)


Regards.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC #007 - 2020 April 1st Ramblings - Valentin Albillo - 04-16-2020 04:41 PM



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