Created
May 28, 2014 03:03
-
-
Save shockalotti/d29021387e8bb3875d28 to your computer and use it in GitHub Desktop.
Go Golang - recursive function, fibonacci sequence
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 fib(n uint) uint { | |
if n == 0 { | |
return 0 | |
} else if n == 1 { | |
return 1 | |
} else { | |
return fib(n-1) + fib(n-2) | |
} | |
} | |
func main() { | |
n := uint(10) | |
fmt.Println(fib(n)) | |
} |
vchakoshy
commented
Feb 6, 2024
package main
import "fmt"
func main() {
var num1 int
fmt.Scan(&num1)
fmt.Println(fibonacci(num1))
}
func fibonacci(n int) int {
fib := make([]int, n+1)
fib[0], fib[1] = 0, 1
for i := 2; i <= n; i++ {
fib[i] = fib[i-1] + fib[i-2]
}
return fib[n]
}```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment