Skip to content

Commit

Permalink
feat: nth Fibonacci number using formula (TheAlgorithms#450)
Browse files Browse the repository at this point in the history
Co-authored-by: Rak Laptudirm <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 5, 2021
1 parent e6cf582 commit 3766420
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

##### Functions:

1. [`Matrix`](./math/fibonacci/fibonacci.go#L11): Matrix This function calculates the n-th fibonacci number using the matrix method. [See](https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form)
1. [`Formula`](./math/fibonacci/fibonacci.go#L42): Formula This function calculates the n-th fibonacci number using the [formula](https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio) Attention! Tests for large values fall due to rounding error of floating point numbers, works well, only on small numbers
2. [`Matrix`](./math/fibonacci/fibonacci.go#L15): Matrix This function calculates the n-th fibonacci number using the matrix method. [See](https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form)

---
</details><details>
Expand Down
13 changes: 13 additions & 0 deletions math/fibonacci/fibonacci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

package fibonacci

import (
"math"
)

// Matrix This function calculates the n-th fibonacci number using the matrix method. [See](https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form)
func Matrix(n uint) uint {
a, b := 1, 1
Expand All @@ -32,3 +36,12 @@ func Matrix(n uint) uint {
}
return uint(rc)
}

// Formula This function calculates the n-th fibonacci number using the [formula](https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio)
// Attention! Tests for large values fall due to rounding error of floating point numbers, works well, only on small numbers
func Formula(n uint) uint {
sqrt5 := math.Sqrt(5)
phi := (sqrt5 + 1) / 2
powPhi := math.Pow(phi, float64(n))
return uint(powPhi/sqrt5 + 0.5)
}
17 changes: 17 additions & 0 deletions math/fibonacci/fibonacci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func TestNthFibonacci(t *testing.T) {
}
}

func TestFormula(t *testing.T) {
tests := getTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := Formula(test.n); test.n <= 10 && got != test.want {
t.Errorf("Return value = %v, want %v", got, test.want)
}
})
}
}

func BenchmarkNthFibonacci(b *testing.B) {
for i := 0; i < b.N; i++ {
dynamic.NthFibonacci(90)
Expand All @@ -64,3 +75,9 @@ func BenchmarkMatrix(b *testing.B) {
Matrix(90)
}
}

func BenchmarkFormula(b *testing.B) {
for i := 0; i < b.N; i++ {
Formula(90)
}
}

0 comments on commit 3766420

Please sign in to comment.