forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fibonacci with matrix (TheAlgorithms#446)
* feat: bitwise min * fix: bitwise min, change for vararg * fix: change min tests * fix: benchmark for bitwise * fix: rename tests * fix: add value param * fix: change condition * feat: added description to some functions * Updated Documentation in README.md * fix: change descriptions * Updated Documentation in README.md * Updated Documentation in README.md * feat: n-th fibonacci number use matrix method * Updated Documentation in README.md * fix: ineffectual assignment to ta (ineffassign) * fix: ineffectual assignment to tb (ineffassign) * Updated Documentation in README.md Co-authored-by: Rak Laptudirm <[email protected]> Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Andrii Siriak <[email protected]>
- Loading branch information
1 parent
8594e11
commit 83832b6
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// fibonacci.go | ||
// description: Get the nth Fibonacci Number | ||
// details: | ||
// In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number) | ||
// author(s) [red_byte](https://github.com/i-redbyte) | ||
// see fibonacci_test.go | ||
|
||
package fibonacci | ||
|
||
// 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 | ||
c, rc, tc := 1, 0, 0 | ||
d, rd := 0, 1 | ||
|
||
for n != 0 { | ||
if n&1 == 1 { | ||
tc = rc | ||
rc = rc*a + rd*c | ||
rd = tc*b + rd*d | ||
} | ||
|
||
ta := a | ||
tb := b | ||
tc = c | ||
a = a*a + b*c | ||
b = ta*b + b*d | ||
c = c*ta + d*c | ||
d = tc*tb + d*d | ||
|
||
n >>= 1 | ||
} | ||
return uint(rc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package fibonacci | ||
|
||
import ( | ||
"github.com/TheAlgorithms/Go/dynamic" | ||
"testing" | ||
) | ||
|
||
func getTests() []struct { | ||
name string | ||
n uint | ||
want uint | ||
} { | ||
tests := []struct { | ||
name string | ||
n uint | ||
want uint | ||
}{ | ||
{"Fibonacci 0-th number == 0", 0, 0}, | ||
{"Fibonacci 1-th number == 1", 1, 1}, | ||
{"Fibonacci 2-th number == 1", 2, 1}, | ||
{"Fibonacci 3-th number == 2", 3, 2}, | ||
{"Fibonacci 4-th number == 3", 4, 3}, | ||
{"Fibonacci 5-th number == 5", 5, 5}, | ||
{"Fibonacci 6-th number == 8", 6, 8}, | ||
{"Fibonacci 7-th number == 13", 7, 13}, | ||
{"Fibonacci 8-th number == 21", 8, 21}, | ||
{"Fibonacci 9-th number == 34", 9, 34}, | ||
{"Fibonacci 10-th number == 55", 10, 55}, | ||
{"Fibonacci 90-th number == 2880067194370816120", 90, 2880067194370816120}, | ||
} | ||
return tests | ||
} | ||
|
||
func TestMatrix(t *testing.T) { | ||
tests := getTests() | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := Matrix(test.n); got != test.want { | ||
t.Errorf("Return value = %v, want %v", got, test.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNthFibonacci(t *testing.T) { | ||
tests := getTests() | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if got := dynamic.NthFibonacci(test.n); 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) | ||
} | ||
} | ||
|
||
func BenchmarkMatrix(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Matrix(90) | ||
} | ||
} |