Firetron's Fib

stable
By Firetron in Miscellaneous Published November 2020 👁 1,363 views 💬 0 comments

Description

Returns the Fibonacci number at the specified index. Test Code:
if not Load('done', false) then

  for i = 0, 30 do
    local fib = CC_Fib(i)
    Log(i..' is '..fib)
  end

  Save('done', true)

end
HaasScript
--  ============================================================================
--    Firetron's Fib
--
--    Returns the Fibonacci number at the specified index.
--
--    Custom Command Dependencies:
--    None
--
--    Discord: @FiretronP75
--  ============================================================================

--  ========================================================
--    Variables
--  ========================================================

--  ------------------------------------
--    Definition
--  ------------------------------------

local defaultValue
local description
local inputSuggestions
local isRequired
local name
local output
local outputSuggestions
local type

--  ------------------------------------
--    Parameter
--  ------------------------------------

local pIndex

--  ------------------------------------
--    Reference
--  ------------------------------------

local firstTwenty = {1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765}

--  ========================================================
--    Command Definition
--  ========================================================

name        = 'Fib'
description = 'Returns the fibonacci number at the specified index.'
DefineCommand(name, description)

--  ========================================================
--    Parameter Definition
--  ========================================================

type             = NumberType
name             = 'index'
description      = 'Index of the fibonacci number to return.'
isRequired       = true
defaultValue     = 1
inputSuggestions = 'Number'
pIndex           = DefineParameter(type, name, description, isRequired, defaultValue, inputSuggestions)

--  ========================================================
--    Functions
--  ========================================================

local GetFibonacciAt = function (index)

  local sqrted  = Sqrt(5)

  local added   = sqrted + 1

  local halved  = added / 2

  local powed   = Pow(halved, index)

  local divided = powed / sqrted

  local rounded = Round(divided, 0)

  return rounded

end

--  ========================================================
--    Execution
--  ========================================================

if pIndex < 1 then

  output = 0

elseif pIndex < 21 then

  output = firstTwenty[pIndex]

else

  output = GetFibonacciAt(pIndex)

end

--  ========================================================
--    Output Definitions
--  ========================================================

type              = NumberType
description       = 'The fibonacci number at the specified index.'
outputSuggestions = 'Trade'
DefineOutput(type, output, description, outputSuggestions)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!