Firetron’s Fib

1 47 Views No Comments 2 weeks ago
  • Returns the fibonacci number at the specified index.

    Test Code:

    function Test ()
      for i = 0, 30 do
        local fib = CC_Fib(i)
        Log(i..' is '..fib)
      end
    end
    
    OptimizedForInterval(60 * 24, Test)
    
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Removed output suggestions
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Removed input suggestions
    • This topic was modified 1 week, 5 days ago by Firetron. Reason: Made first twenty pre-calculated for efficiency
    HaasScript Code
    --  ============================================================================
    --    Firetron's Fib
    --
    --    Returns the fibonacci number at the specified index.
    --
    --    Custom Command Dependencies:
    --    None
    --
    --    Discord: @FiretronP75
    --  ============================================================================
    
    --  ========================================================
    --    Variables
    --  ========================================================
    
    --  ------------------------------------
    --    Definition
    --  ------------------------------------
    
    local description
    local inputSuggestions
    local output
    local outputSuggestions
    
    --  ------------------------------------
    --    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
    --  ========================================================
    
    description = 'Returns the fibonacci number at the specified index.'
    DefineCommand('Fib', description)
    
    --  ========================================================
    --    Parameter Definition
    --  ========================================================
    
    description      = 'Index of the fibonacci number to return.'
    inputSuggestions = ''
    pIndex           = DefineParameter(NumberType, 'index', description, true, 1, 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
    --  ========================================================
    
    description       = 'The fibonacci number at the specified index.'
    outputSuggestions = ''
    DefineOutput(NumberType, output, description, outputSuggestions)
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free