Created
September 12, 2023 16:24
-
-
Save gabrielbussolo/109f3f104a1573bf1286458ac2e7d51f to your computer and use it in GitHub Desktop.
Fibonacci implementation interactive and recursive
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 fibonacci(num int) int { | |
if num == 0 { | |
return 0 | |
} | |
if num == 1 { | |
return 1 | |
} | |
return fibonacci(num-1) + fibonacci(num-2) | |
} | |
func fibonacciI(num int) int { | |
fib := []int{0, 1} | |
for i := 2; i < num+1; i++ { | |
fib = append(fib, fib[i-1]+fib[i-2]) | |
} | |
return fib[num] | |
} | |
func main() { | |
fmt.Println(fibonacci(9)) | |
fmt.Println(fibonacciI(9)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment