Recursion is an algorithm in which one of the steps invokes the currently running method or function. This algorithm acquires the outcome for the input by applying basic tasks and then returns the value. This method was briefly discussed in the Divide and conquer algorithms section of Chapter 1, Data Structures and Algorithms. During recursion, if the base condition is not reached, then a stack overflow condition may arise.
A recursion algorithm is implemented in the following code snippet. The Factor method takes the num as a parameter and returns the factorial of num. The method uses recursion to calculate the factorial of the number:
//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt and bytes package
import (
"fmt"
)
//factorial method
func Factor(num int) int {
if num <= 1 {
return 1
}
return...