diff --git a/algo/fibonacci/fibonacci.go b/algo/fibonacci/fibonacci.go index 4746c13..b479e3c 100644 --- a/algo/fibonacci/fibonacci.go +++ b/algo/fibonacci/fibonacci.go @@ -5,8 +5,8 @@ // []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233...} package fibonacci -// Dynamic utilizes memoization and dynamic programming principle, -// to calculate the the fibonacci number for a given index. +// Dynamic utilizes memoization and dynamic programming principles, +// to calculate the fibonacci number for a given index. func Dynamic() func(n int) int { var fib func(int) int @@ -30,6 +30,7 @@ func Dynamic() func(n int) int { } // BottomUp is a bottom up approach to finding the number. +// Runtime complexity O(n). func BottomUp(n int) int { answer := []int{0, 1} for i := 2; i <= n; i++ { @@ -39,7 +40,8 @@ func BottomUp(n int) int { return answer[len(answer)-1] } -// Basic function is simple implementation. +// Basic function is simple implementation, that uses recursion. +// Runtime complexity O(n^2). func Basic(n int) int { if n < 2 { return n diff --git a/algo/fibonacci/fibonacci_test.go b/algo/fibonacci/fibonacci_test.go index 11dfa04..bcdd413 100644 --- a/algo/fibonacci/fibonacci_test.go +++ b/algo/fibonacci/fibonacci_test.go @@ -6,19 +6,27 @@ import ( "github.com/eng618/go-eng/algo/fibonacci" ) -func Example() { - // Fibonacci example - fibO := fibonacci.Dynamic() - fmt.Println(fibO(10)) - - // FibonacciBU example - fmt.Println(fibonacci.BottomUp(10)) - +func ExampleBasic() { // Fib example fmt.Println(fibonacci.Basic(10)) // Output: // 55 +} + +func ExampleBottomUp() { + // FibonacciBU example + fmt.Println(fibonacci.BottomUp(10)) + + // Output: // 55 +} + +func ExampleDynamic() { + // Fibonacci example + fibO := fibonacci.Dynamic() + fmt.Println(fibO(10)) + + // Output: // 55 }