Skip to content

Instantly share code, notes, and snippets.

@joshuabezaleel
Created April 13, 2020 10:28
Show Gist options
  • Save joshuabezaleel/21e59cfe11b6078617069db65a32ca9c to your computer and use it in GitHub Desktop.
Save joshuabezaleel/21e59cfe11b6078617069db65a32ca9c to your computer and use it in GitHub Desktop.
func calculate(input ...int, method string) int {
var result int
if method == "fibonacci" {
result = fibRec(input[0])
}
return result
}
func fibRec(n int) int {
if n <= 1 {
return n
} else {
return fibRec(n-1) + fibRec(n-2)
}
}
}
func fibNonRec(n int) int {
if n <= 1 {
return n
}
var fib = 1
var fibPrev = 1
for i := 2; i < n; i++ {
var temp int
temp = fib
fib = fib + fibPrev
fibPrev = temp
}
return fib
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment