Go Language Program to Find the Nth Fibonacci Number


Fibonacci numbers have a unique position in mathematics, computer science and even nature for its particular mathematical qualities. Each number in this series represents the addition of the 2 previous ones, starting at 0 and 1. In this article, we are going to explore an approach to find the Nth Fibonacci number to go efficiently. We will explain two examples in the first example we used the recursive approach as it is easy to implement and fast for moderate values of n, but may be slow for large inputs. In the second example we are going to use the iterative approach.

Explanation

Each number in the series of Fibonacci numbers is the sum of 2 previous numbers in the sequence. The series starts at 0 and 1 in mathematics. Numerous natural occurrences, from the patterning of leaves on a stem to the spirals of a conch, demonstrate the interesting features of these numbers.

0

1

1

2

3

5

8

13

21

...

This is the representation of a fibonacci series, it start with 0 and 1, and each subsequent, number is sum of the two preceding numbers, Like 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3 2 + 3 = 5 and so on.

Syntax

func fibonacciRecursive(n int) int

The syntax defines a function named `fibonacciRecursive` which calculates the Nth Fibonacci number following the mathematical definition of the Fibonacci sequence, summing the two preceding numbers.

func fibonacciIterative(n int) int

The syntax defines a function fibonacciIterative to iteratively compute the Nth Fibonacci number, beginning with the first two Fibonacci numbers (0 and 1).

Algorithm

  • First, set values of 3 variables—a, b, and result—to default. To begin with the Fibonacci sequence, set a and b to 1 and 0 correspondingly.

  • For every i between 2 and N, do the steps below:

    • Set result as the sum of a and b.

    • Update a with the value of b.

    • Update b with the value of result.

  • After the loop, result will hold the Nth Fibonacci number.

Example 1

In this example, we will Find the Nth Fibonacci Number in go Using recursive formula fibonacciRecursive function can get the nth Fibonacci number calculating in a recursive procedure. In simple case when n = 1 we return 1. If n is more than n-1 return n-1 plus fibonacciRecursive(n-1).

package main
import "fmt"
func fibonacciRecursive(n int) int {
	if n <= 1 {
    	return n
	}
	return fibonacciRecursive(n-1) + fibonacciRecursive(n-2)
}
func main() {
	n := 6
	fmt.Printf("Using the Recursive method:\n")
	fmt.Printf("The %dth Fibonacci number is: %d\n", n, fibonacciRecursive(n))
}

Output

Using the Recursive method:
The 6th Fibonacci number is: 8

Example 2

In this example,we will Find the Nth Fibonacci Number in go , the iterative computation of Fibonacci numbers begins with the first 2 and the function fibonacciIterative does this. The n-th Fibonacci number is obtained by iteration as 2 variables first and second are repeatedly changed while n is held constant until the desired result is achieved.

package main
import "fmt"
func fibonacciIterative(n int) int {
	if n <= 1 {
    	return n
	}
	first, second := 0, 1
	for i := 2; i <= n; i++ {
    	first, second = second, first+second
	}
	return second
}
func main() {
	n := 6
	fmt.Printf("Using the Iterative method:\n")
	fmt.Printf("The %dth Fibonacci number is: %d\n", n, fibonacciIterative(n))
}

Output

Using the Iterative method:
The 6th Fibonacci number is: 8

Real Life Implementation

  • Art and Design: Practitioners in the area of Art & Design often employ the Fibonacci sequence as a technique to generate visually beautiful compositions. The ratios in the sequence, known as the Golden Ratio, are usually considered to have visually pleasing proportions. The approach has been used to produce visually pleasing compositions in architecture, photography, and graphic design.

  • Rabbit Population Growth: The Fibonacci sequence may be used to illustrate certain population growth situations, such as reproductive trends in rabbit populations. Rabbit populations follow the Fibonacci pattern because each generation is composed of two rabbits born from the same set of parents.

Conclusion

Fibonacci numbers are a series of numbers starting at 0 and 1 in which each number equals the sum of 2 preceding numbers. In this article, we saw 2 different methods to Find the Nth Fibonacci Number in go. The recursive approach is easy to implement and fast for moderate values of n, but may be slow for large inputs. However, when working with bigger values of n, the iterative technique is more efficient and is thus suggested for locating Fibonacci numbers.

Updated on: 18-Oct-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements