diff options
-rw-r--r-- | entries/vivkpatl/gofib.go | 41 | ||||
-rw-r--r-- | people.json | 11 |
2 files changed, 52 insertions, 0 deletions
diff --git a/entries/vivkpatl/gofib.go b/entries/vivkpatl/gofib.go new file mode 100644 index 0000000..293beaf --- /dev/null +++ b/entries/vivkpatl/gofib.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" +) + +func main() { + var n int + + fmt.Printf("Gimme an int: ") + + _, err := fmt.Scanf("%d", &n) + if err != nil { + panic(err) + } + + if n <= 0 { + panic("n must be greater than 0!") + } + + cache := make([]int, n + 1) + for i := 0; i <= n; i++ { + cache[i] = -1; + } + + fmt.Printf("fib(%d) = %d\n", n, fib(n, cache)) +} + +func fib(n int, cache []int) int { + if n < 2 { + return n + } + + if (cache[n] != -1) { + return cache[n] + } + + cache[n] = fib(n - 1, cache) + fib(n - 2, cache) + + return cache[n] +} diff --git a/people.json b/people.json index defd08b..67424df 100644 --- a/people.json +++ b/people.json @@ -722,6 +722,17 @@ ] }, + { + "github": "vivkpatl", + "name": "Vivek Patel", + "title": "Impostor Syndrome", + "entries": [ + { + "name": "gofib", + "link": "./entries/vivkpatl/gofib.go" + } + ] + }, { "github": "williamvietnguyen", "name": "William Nguyen", |