Created
April 13, 2020 10:28
-
-
Save joshuabezaleel/21e59cfe11b6078617069db65a32ca9c to your computer and use it in GitHub Desktop.
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
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