# _Fibonacci() - Calculate the Fibonacci number of a decimal value.

## Recommended Posts

This function will calculate the Finonacci number based on a decimal value.

Function:

```; #FUNCTION# ====================================================================================================================
; Name ..........: _Fibonacci
; Description ...: Calculate the Fibonacci number of a decimal value.
; Syntax ........: _Fibonacci(\$iIterations)
; Parameters ....: \$iIterations - An integer value to iterate to.
; Return values .: Success - Fibonacci value
; Author ........: guinness & UEZ
; Link ..........: C++ example: https://en.wikipedia.org/wiki/Functional_programming#Coding_styles
; Example .......: Yes
; ===============================================================================================================================
Func _Fibonacci(\$iIterations)
\$iIterations = Int(\$iIterations)
Local \$iFirst = 0, \$iSecond = 1, \$iSum = 0
If \$iIterations > 92 Then
\$iSecond = 1.0
EndIf
For \$i = 0 To (\$iIterations - 1)
\$iSum = \$iFirst + \$iSecond
\$iFirst = \$iSecond
\$iSecond = \$iSum
Next
Return \$iFirst
EndFunc   ;==>_Fibonacci```
Function V3.9.9.4+

```; #FUNCTION# ====================================================================================================================
; Name ..........: _Fibonacci
; Description ...: Calculate the Fibonacci number of a decimal value.
; Syntax ........: _Fibonacci(\$iIterations)
; Parameters ....: \$iIterations - An integer value to iterate to.
; Return values .: Success - Fibonacci value
; Author ........: guinness & trancexx
; Link ..........: C++ example: https://en.wikipedia.org/wiki/Functional_programming#Coding_styles
; Example .......: Yes
; ===============================================================================================================================
Func _Fibonacci(\$iIterations)
\$iIterations = Int(\$iIterations)
Local \$iFirst = 0, \$iSecond = (\$iIterations > 92) ? 1.0 : 1, \$iSum = 0
For \$i = 0 To (\$iIterations - 1)
\$iSum = \$iFirst + \$iSecond
\$iFirst = \$iSecond
\$iSecond = \$iSum
Next
Return \$iFirst
EndFunc   ;==>_Fibonacci```
Example use of Function:

```For \$i = 0 To 20
ConsoleWrite(\$i & ' >> ' & _Fibonacci(\$i) & @CRLF)
Next```
Edited by guinness
##### Share on other sites

I would suggest to insert the following line as first line of the function.

`If \$iIterations < 0 Or \$iIterations > 92 Then Exit MsgBox(262144, "_Fibonacci Func", "Iteration value '" & \$iIterations & "' out of bounds (0 to 92)", 0)`

App: Au3toCmd              UDF: _SingleScript()

##### Share on other sites

I would suggest to insert the following line as first line of the function.

`If \$iIterations < 0 Or \$iIterations > 92 Then Exit MsgBox(262144, "_Fibonacci Func", "Iteration value '" & \$iIterations & "' out of bounds (0 to 92)", 0)`

True, though as you know functions should simply return, not Exit or invoke a MsgBox of some description.
##### Share on other sites

Well, you can always do something like this:

`\$iSecond = (\$iIterations > 92) ? 1.0 : 1`

...and then calculate _Fibonacci(1476)

.

eMyvnE

##### Share on other sites

`\$iSecond = (\$iIterations > 92) ? 1.0 : 1`

What's with the ? 1.0 : 1

Edited by spudw2k
Spoiler

Misc Code Snippets:
Projects: SubnetCalc
Cool Stuff:

##### Share on other sites

It's the new ternary notation in the beta version.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

I think the line must be

`\$iSecond = (\$iIterations > 92) ? 1.0 * \$iSum : 1 * \$iSum`

to work properly (on alpha version).

It switches from integer to floating point.

```ConsoleWrite(_Fibonacci(1000) & @LF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _Fibonacci
; Description ...: Calculate the Fibonacci number of a decimal value.
; Syntax ........: _Fibonacci(\$iIterations)
; Parameters ....: \$iIterations - An integer value to iterate to.
; Return values .: Success - Fibonacci value
;                 Failure - -1 & sets @error to non-zero.
; Author ........: guinness
; Link ..........: C++ example: https://en.wikipedia.org/wiki/Functional_programming#Coding_styles
; Example .......: Yes
; ===============================================================================================================================
Func _Fibonacci(\$iIterations)
If IsNumber(\$iIterations) = 0 Then
\$iIterations = Number(\$iIterations)
EndIf
;~   If \$iIterations < 0 Or \$iIterations > 92 Then
;~       Return SetError(1, 0, -1)
;~   EndIf
Local \$iFirst = 0, \$iSecond = 1, \$iSum = 0
For \$i = 0 To (\$iIterations - 1)
\$iSum = \$iFirst + \$iSecond
\$iFirst = \$iSecond
If \$iIterations > 92 Then
\$iSecond = \$iSum * 1.0
Else
\$iSecond = \$iSum
EndIf
Next
Return \$iFirst
EndFunc   ;==>_Fibonacci```

Floating point can handle 64bit numbers...

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

I have to say this example by UEZ is really neat. Ah UEZ is here:

I like both examples.

Edited by czardas

##### Share on other sites

@guinness: the code in post#8 is also working with current release (3.3.8.1).

Here some more variants:

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

I think the line must be

`\$iSecond = (\$iIterations > 92) ? 1.0 * \$iSum : 1 * \$iSum`

to work properly (on alpha version).

Actually no. More correct is to do it just the way I said - when declaring. You don't want unnecessary evaluations and mathematical operations inside the loop. Edited by trancexx

.

eMyvnE

##### Share on other sites

Actually no. More correct is to do it just the way I said - when declaring. You don't want unnecessary evaluations and mathematical operations inside the loop.

Yep, you are right again.

@guinness: maybe you can add also the recursive variant to your 1st post to complete iterative and recursive variants.

```\$n = 1000

ConsoleWrite(Fibonacci_r2(\$n) & @CRLF)

Func Fibonacci_r2(\$f)
If \$f < 2 Then Return \$f
If \$f > 1476 Then Return 0
Return Fibonacci_r2_(1, 1, \$f)
EndFunc

Func Fibonacci_r2_(\$f1, \$f2, \$fn)
If \$fn < 3 Then Return \$f2
Return Fibonacci_r2_(\$f2, \$f1 * 1.0 + \$f2 * 1.0, \$fn - 1)
EndFunc```

As far as I can remember the code above was written by AspirinJunkie but I'm not sure...

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Nice work guinness!

Here's my version

```Func _Fibonacci(\$iIterations)
Return (5 ^ -0.5) * ((((1 + (5 ^ 0.5)) / 2) ^ \$iIterations) - (((1 - (5 ^ 0.5)) / 2) ^ \$iIterations))
EndFunc```
Edited by n1maS
##### Share on other sites

• 5 months later...

Updated the function by removing the exclusive check for whether \$iIterations is an integer and instead just cast as an Int.

Edited by guinness

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...