Fibonacci Numbers

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
Hi,

This is a short program to output a fibonacci sequence. Can anyone think of another way to create this algorithm? The problem is one of the questions at the end of a chapter in a book about programming. Assume you have only the most basic flow control knowledge and can only use primitive types (technically I'm not even supposed to know how to declare an array yet, either).

Code:
[COLOR="Navy"]Sub[/COLOR] FibNums()
[COLOR="Navy"]Dim[/COLOR] arr(2) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nextNum [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] x [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] rsp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] msg [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]

    [COLOR="SeaGreen"]'//Seed First Two Fibonacci numbers[/COLOR]
    arr(0) = 0
    arr(1) = 1

    [COLOR="SeaGreen"]'//User inputs how many Fibonacci numbers to list[/COLOR]
    rsp = CLng(Application.InputBox("How many Fibonacci numbers do you want [enter a number zero or greater]:"))
    [COLOR="Navy"]If[/COLOR] CLng(rsp) > 0 [COLOR="Navy"]Then[/COLOR]
        msg = "1"
        [COLOR="Navy"]For[/COLOR] x = 2 [COLOR="Navy"]To[/COLOR] CLng(rsp)
            nextNum = arr(0) + arr(1)
            msg = msg & " " & nextNum
            arr(0) = arr(1)
            arr(1) = nextNum
        [COLOR="Navy"]Next[/COLOR] x
        MsgBox msg
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I'm surprised that this works:

Code:
b1() = "0" & a1
 
Upvote 0
My understanding that for the "0" & a1 expression VBA creates runtime Variant/String variable thus it works properly
 
Upvote 0
I meant the assignment to the Byte array, rather than using StrConv.
 
Upvote 0
Assignment to the Byte array is working and it's nice method for fast processing.
Usage of SafeArray API can be even faster but not so simple.
Surely the code of Fib5 and Fib6 can be more optimized but for me the speed of Byte array processing is good enough.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,661
Messages
6,186,288
Members
453,348
Latest member
newbieBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top