mirror of
https://github.com/avelino/awesome-go.git
synced 2024-11-14 16:42:23 +00:00
24 lines
461 B
Go
24 lines
461 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
func fibonacci(n int) int {
|
|
// Create a memoization table to store previously computed values
|
|
memo := make([]int, n+1)
|
|
|
|
// Base cases
|
|
memo[0] = 0
|
|
memo[1] = 1
|
|
|
|
// Compute Fibonacci sequence using dynamic programming
|
|
for i := 2; i <= n; i++ {
|
|
memo[i] = memo[i-1] + memo[i-2]
|
|
}
|
|
|
|
return memo[n]
|
|
}
|
|
|
|
func main() {
|
|
n := 10
|
|
fmt.Printf("Fibonacci(%d) = %d\n", n, fibonacci(n))
|
|
} |