Go is a modern programming language developed by Google. It was created to be efficient, concise, and easy to use. Go combines the best features of other programming languages, balancing simplicity and powerful performance.
Factorial is a mathematical operation denoted by the exclamation mark '!', where a non-negative number is multiplied by all the positive integers below it, down to 1.
For example, the factorial of six is:
Another essential fact about factorials is that
To compute the factorial of a given number in Go, two methods are available: the recursive approach and the iterative approach. Both ways are feasible but differ in the execution of the code and function calls.
package mainimport ("fmt""log")// Recursive approach to calculate factorialfunc factorialRecursive(n int) int {// Base case: factorial of 0 or 1 is 1if n == 0 || n == 1 {return 1}// Recursive call to calculate factorialreturn n * factorialRecursive(n-1)}func main() {// Calculate factorial using recursive approachvar number int_, err := fmt.Scanln(&number)if err != nil {log.Fatal("Invalid input. Please enter a valid non-negative integer.")}if number < 0 {log.Fatal("Invalid input. Please enter a non-negative integer.")}// Calculate factorial using recursive approachfactorialRec := factorialRecursive(number)fmt.Println("Factorial of", number, "(Recursive):", factorialRec)}
Enter the input below
Lines 7–14: The recursive function factorialRecursive
accepts an integer input and has a base case that returns 1 when the number is 0 or 1. The function uses a recursive approach, which means it calls itself with 1 number lower than the present until it reaches the base case. The result is then multiplied recursively, propagating the multiplication back to the root call.
Lines 17–24: The validation process occurs where input is obtained from the user, validated to ensure compatibility with the expected data type and that it is a non-negative integer.
Line 26: The recursive function is called, and the value returned is saved in the variable factorialRec
.
package mainimport ("fmt""log")// Iterative approachfunc factorialIterative(n int) int {factorial := 1// Multiply numbers from 2 to n to calculate factorialfor i := 2; i <= n; i++ {factorial *= i}return factorial}func main() {// Calculate factorial using iterative approachvar number int_, err := fmt.Scanln(&number)if err != nil {log.Fatal("Invalid input. Please enter a valid non-negative integer.")}if number < 0 {log.Fatal("Invalid input. Please enter a non-negative integer.")}// Calculate factorial using iterative approachfactorialIter := factorialIterative(number)fmt.Println("Factorial of", number, "(Iterative):", factorialIter)}
Enter the input below
Lines 7–14: The iterative function factorialIterative
accepts an integer input and uses a for loop to perform a sequence of multiplications from the smallest number to the input number. If the input number is 0 or 1, the function returns 1, as the factorial of 0 or 1 is defined as 1.
Line 26: The iterative function is called, and the value returned is saved in the variable factorialIter
.
The recursive and iterative approaches require either iterating over a range from 1 to N or making approximately N function calls in the case of the recursive process. Consequently, the time complexity for both approaches can be considered as O(N).
Regarding space complexity, neither of the methods requires storing the intermediate results in an array or any additional data structure. As a result, the space complexity for both approaches can be considered as O(1), indicating constant space usage.
Recursive | Iterative | |
Time Complexity | O(N) | O(N) |
Space Complexity | O(1) | O(1) |
There are two different approaches used to calculate the factorial of a number. While the underlying algorithm is conceptually similar to other programming languages, Go has unique syntax and constructs.
Free Resources