Skip to content

Commit 5484469

Browse files
authored
Create fibonacci_memoization.go
1 parent 513070d commit 5484469

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/go/fibonacci_memoization.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "fmt"
4+
5+
var memo = make(map[int]int)
6+
7+
func FibonacciMemoization(n int) int {
8+
if n <= 1 {
9+
return 1
10+
}
11+
12+
// Check if the value is already memoized
13+
if val, ok := memo[n]; ok {
14+
return val
15+
}
16+
17+
// Calculate Fibonacci recursively and store the result in memoization
18+
result := FibonacciMemoization(n-1) + FibonacciMemoization(n-2)
19+
memo[n] = result
20+
return result
21+
}
22+
23+
func main() {
24+
n := 9
25+
fmt.Println("Fibonacci Memoization:", FibonacciMemoization(n))
26+
}

0 commit comments

Comments
 (0)