Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shockalotti/d29021387e8bb3875d28 to your computer and use it in GitHub Desktop.
Save shockalotti/d29021387e8bb3875d28 to your computer and use it in GitHub Desktop.
Go Golang - recursive function, fibonacci sequence
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))
}
@mygedz
Copy link

mygedz commented Feb 27, 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