Skip to content

Commit 2e62848

Browse files
geeksforgeeks: add fibonacci
1 parent 4c60fa7 commit 2e62848

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

geeksforgeeks/fibonacci/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Fibonacci
2+
3+
Given a number n, find n-th Fibonacci Number.
4+
5+
## Copyright Notice
6+
7+
This problem is based on [content](https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/)
8+
from [GeeksforGeeks](https://www.geeksforgeeks.org)
9+
written by GeeksforGeeks
10+
and subject to [GeeksforGeeks copyright](https://www.geeksforgeeks.org/legal/copyright-information/).
11+
The original content from GeeksforGeeks and any modifications made here are attributed to GeeksforGeeks contributors,
12+
and this work is shared under [CC BY-SA 4.0](../LICENSE).

geeksforgeeks/fibonacci/solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fibonacci
2+
3+
func Solution(n int) int {
4+
memoization := make([]int, n+1)
5+
return fib(n, memoization)
6+
}
7+
8+
func fib(n int, memoization []int) int {
9+
if n <= 1 {
10+
return n
11+
}
12+
13+
if number := memoization[n]; number != 0 {
14+
return number
15+
}
16+
17+
memoization[n] = fib(n-1, memoization) + fib(n-2, memoization)
18+
return memoization[n]
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package fibonacci_test
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
"github.com/minizilla/minmax/geeksforgeeks/fibonacci"
8+
"github.com/minizilla/testr"
9+
)
10+
11+
func TestFibonacci(t *testing.T) {
12+
fib := []int{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
13+
14+
for n, number := range fib {
15+
t.Run(strconv.Itoa(n), func(t *testing.T) {
16+
assert := testr.New(t)
17+
out := fibonacci.Solution(n)
18+
assert.Equal(out, number)
19+
})
20+
}
21+
}

0 commit comments

Comments
 (0)