fibonacci

package
v0.12.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package fibonacci allows you to calculate which fibonacci number would be at a given index. Imagine the fibonacci sequence is represented in the form of a slice of ints, similar to the below example.

Fibonacci Sequence
[]int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233...}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Basic

func Basic(n int) int

Basic function is simple implementation, that uses recursion. Runtime complexity O(n^2).

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/algo/fibonacci"
)

func main() {
	// Fib example
	fmt.Println(fibonacci.Basic(10))

}
Output:

55

func BottomUp

func BottomUp(n int) int

BottomUp is a bottom up approach to finding the number. Runtime complexity O(n).

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/algo/fibonacci"
)

func main() {
	// FibonacciBU example
	fmt.Println(fibonacci.BottomUp(10))

}
Output:

55

func Dynamic

func Dynamic() func(n int) int

Dynamic utilizes memoization and dynamic programming principles, to calculate the fibonacci number for a given index.

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/algo/fibonacci"
)

func main() {
	// Fibonacci example
	fibO := fibonacci.Dynamic()
	fmt.Println(fibO(10))

}
Output:

55

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL