Skip to content

Commit

Permalink
feat: fibonacci with matrix (TheAlgorithms#446)
Browse files Browse the repository at this point in the history
* 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
4 people authored Nov 25, 2021
1 parent 8594e11 commit 83832b6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
2. [`Recursive`](./math/factorial/factorial.go#L21): Recursive This function recursively computes the factorial of a number
3. [`UsingTree`](./math/factorial/factorial.go#L30): UsingTree This function finds the factorial of a number using a binary tree

---
</details><details>
<summary> <strong> fibonacci </strong> </summary>

---

##### 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)

---
</details><details>
<summary> <strong> gcd </strong> </summary>
Expand Down Expand Up @@ -781,6 +791,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

1. [`New`](./structure/set/set.go#L7): New gives new set.

---
</details><details>
<summary> <strong> sha256 </strong> </summary>

---

##### Functions:

1. [`Hash`](./hashing/sha256/sha256.go#L50): Hash hashes the input message using the sha256 hashing function, and return a 32 byte array. The implementation follows the RGC6234 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc6234

---
</details><details>
<summary> <strong> sort </strong> </summary>
Expand Down
34 changes: 34 additions & 0 deletions math/fibonacci/fibonacci.go
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)
}
66 changes: 66 additions & 0 deletions math/fibonacci/fibonacci_test.go
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)
}
}

0 comments on commit 83832b6

Please sign in to comment.