Domino Code Fragment

Code Name*
Fibonacci Sequence of n using iteration
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.3.144.97.189
Description*
Returns Fibonacci Sequence of n using iteration
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
It should be emphasized that this iterative procedure is much more efficient than the recursive procedure in the " Fibonacci Sequence of n using recursion" document.
Files/Graphics attachments (if applicable): Code:
The celebrated Fibonacci Sequence (usually denoted by F0,F1,F2,F3,...) is as follows
 0,1,1,2,3,5,8,13,21,34,55,...

That is F0 = 0 and F1 = 1 and each succeeding term is the sum of the two preceding terms.
For example, the next two terms of the sequence are 34 + 55 = 89 and 55 + 89 = 144

The formal definition of this function follows:
(a) If n = 0 or n = 1, then Fn = n.
(b) if n > 1, then Fn = Fn-2 + Fn-1

 

Public Function Fibonacci (n As Integer) As Long
    Dim x As Integer
    Dim fib () As Variant
    Redim fib (1 To 2)
    fib(1) = 1
    fib(2) = 1
    If n  > 0 Then
         If n <= 1 Then                  
              Fibonacci = n
              Exit Function
         End If
         Redim Preserve fib (1 To n)
         For i = 3 To n
              fib(i) = fib(i-1) + fib(i - 2)
         Next i
         x = Ubound(fib)
         Fibonacci = fib(x)
    End If
End Function