Golang program to return the nth number in fibonacci sequence using dynamic programming


In this article, we will write Go language programs to return the nth number in Fibonacci sequence using dynamic programming. It is a technique used to solve complex problems by breaking them into smaller sub problems.

Memoization is the process of storing the output of the function calls in some data structure by which the next time call is made it need not calculate the output again, it can use that value to make the computation which in return lessens the execution time.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.

Algorithm

  • This program imports the main and fmt package.

  • Create a function named fibo(n int) int that takes an integer n as input parameter and returns the desired Fibonacci number.

  • Create a slice fib of size n+1 to store the Fibonacci numbers using make as built-in function.

  • Set the 0th and the 1th Fibonacci numbers.

  • In this step, iterate from 2 to n using a for loop where in every iteration calculate the ith Fibonacci.

  • After the loop is terminated, return fib[n] as the output.

  • Create a main function in which set the nth number whose Fibonacci value is to be calculated.

  • Then, call the fibo() function with n as the argument to calculate the output.

  • Print the output on the console using the printf function from the fmt package.

Example 1

In this example, we will write a Golang program to find nth Fibonacci number by adding the two previous Fibonacci numbers to get the current number and store it in a slice created using make function.

package main
import "fmt"

func fibo(n int) int {
	fib := make([]int, n+1)
	fib[0] = 0
	fib[1] = 1

	for i := 2; i <= n; i++ {
		fib[i] = fib[i-1] + fib[i-2]
	}
	return fib[n]
}
func main() {
	n := 6
	output := fibo(n)
	fmt.Printf("The %dth number in this Fibonacci sequence is: %d\n", n, output)
}

Output

The 6th number in this Fibonacci sequence is : 8

Example 2

In this example, we will write a Golang program to return the nth number in Fibonacci sequence by using a map that stores the Fibonacci numbers calculated using recursion.

package main
import "fmt"
var store map[int]int
func fibo(n int) int {
	if val, ok := store[n]; ok {
		return val
	}
	if n == 0 {
		store[n] = 0
		return 0
	} else if n == 1 {
		store[n] = 1
		return 1
	}
	fib := fibo(n-1) + fibo(n-2)
	store[n] = fib
	return fib
}
func main() {
	n := 8
	store = make(map[int]int)
	output := fibo(n)
	fmt.Printf("The %dth number in the Fibonacci sequence is: %d\n", n, output)
}

Output

The 8th number in the Fibonacci sequence is: 21

Conclusion

We compiled and executed the program of returning the nth number in Fibonacci sequence using dynamic programming with the help of two approaches. In the first approach, we used dynamic programming and in the second approach we used memoization with dynamic programming.

Updated on: 04-Aug-2023

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements