Golang program to compute the factorial of a number using concurrency


In this article, we will write Go language programs to compute the factorial of a number using concurrency. It is a task of implementing multiple operations simultaneously and can be implemented using Go routines and channels. Go routines are the lightweight threads and channels help in non-conflicting communications between the routines.

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 necessary packages main and fmt.

  • In this step, define the function factorial_number that takes input parameter n of type uint64 and a channel ch of type chan uint64.

  • Then, create a function named factorial_number to calculate the factorial of a number.

  • Then, initialize a variable fact of type uint64 to 1.

  • Then, use a for loop from 2 until i reaches n. In each iteration multiply fact by i.

  • Then, send the calculated factorial value stored in fact to the channel ch.

  • Create a main function.

  • In the main, firstly set the number whose factorial is to be calculated.

  • In this step, create a channel ch of type chan uint64 using make which is a built-in function.

  • Then, start a go routine to calculate the factorial by calling the function factorial_number with the number and ch as argument.

  • Then, read the factorial calculated from the channel and store it inside fact variable.

  • Finally, print the output on the console using the Printf function with the use of %d to print numerical values.

Example 1

In this example, we will write a Golang program to calculate the factorial of a number using go routines and channels to execute concurrent implementation of the process.

package main

import (
	"fmt"
)
func factorial_number(n uint64, ch chan uint64) {
	fact := uint64(1)
	for i := uint64(2); i <= n; i++ {
		fact *= i
	}
	ch <- fact
}
func main() {
	number := uint64(6)
	ch := make(chan uint64)
	go factorial_number(number, ch)
	fact := <-ch
	fmt.Printf("Factorial of %d is: %d\n", number, fact)
}

Output

Factorial of 6 is : 720

Example 2

In this example, we will write a Golang program to calculate the factorial of a number by using the recursive technique with Go routines and channels.

package main
import (
	"fmt"
)
func factorial_number(n uint64, ch chan uint64) {
	if n == 0 {
		ch <- 1
		return
	}
	fact := n
	for i := n - 1; i > 0; i-- {
		fact *= i
	}
	ch <- fact
}
func main() {	
	number := uint64(8)	
	ch := make(chan uint64)	
	go factorial_number(number, ch)	
	fact := <-ch	
	fmt.Printf("Factorial of %d is: %d\n", number, fact)
}

Output

Factorial of 8 is: 40320

Conclusion

We compiled and executed the program of computing the factorial of a number using two examples. In the first example we used go routines and channels and in the second example, we used recursion with these go routines and channels.

Updated on: 04-Aug-2023

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements