Skip to content

Instantly share code, notes, and snippets.

@danielrangelmoreira
Last active November 17, 2020 12:41
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Fastest, coolest, fibonacci algorithm golang with dynamic programming
package main
import (
"fmt"
"math/big"
)
func fib(n int) *big.Int {
fn := make([]*big.Int, n+1)
for i := 0; i <= n; i++ {
var f = big.NewInt(0)
if i <= 2 {
f.SetUint64(1)
} else {
f = f.Add(fn[i-1], fn[i-2])
}
fn[i] = f
}
return fn[n]
}
func main() {
fmt.Println(fib(300))
}
@diegogaulke
Copy link

Thank you.
I will use it in my project with Gin framework, to show the performance of go with fib and gin framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment