Last active
January 1, 2018 23:31
-
-
Save dougyoung/74b659c158dcf3b3dd80597bcfec0a1b to your computer and use it in GitHub Desktop.
Dynamic programming implementation of the Fibonacci sequence, time complexity of O(n).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func fibo(x uint64, y uint64, n uint64) uint64 { | |
fibos := [2]uint64{x, y} | |
var fibo uint64; | |
for i := uint64(2); i < n+1; i++ { | |
fibo = fibos[0] + fibos[1] | |
fibos[0] = fibos[1] | |
fibos[1] = fibo | |
} | |
return fibo | |
} | |
func main() { | |
var count int; | |
_, err := fmt.Scanf("%d", &count) | |
if err != nil { | |
panic(err) | |
} | |
for i := 0; i < count; i++ { | |
var x, y, n uint64; | |
fmt.Scanf("%d %d %d", &x, &y, &n) | |
f := fibo(x, y, n) | |
fmt.Println(f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment