How to resolve the algorithm Factorial step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Factorial step by step in the Go programming language

Table of Contents

Problem Statement

Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative   n   errors is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factorial step by step in the Go programming language

1- The first code snippet:

  • The code snippet implements the factorial function using the big package to handle large integers.
  • The function factorial takes an integer n as input and returns the factorial of that number as a big.Int.
  • It initializes a big.Int variable r to 1 and loops through the numbers from 2 to n, multiplying r by the current number in each iteration.
  • Finally, it returns the value of r.

2- The second code snippet:

  • The code snippet implements the factorial function using the MulRange method of the big package.
  • The function factorial takes an integer n as input and returns the factorial of that number as a big.Int.
  • It initializes a big.Int variable z and uses the MulRange method to compute the product of the numbers from 1 to n.
  • Finally, it returns the value of z.

3- The third code snippet:

  • The code snippet implements the factorial function using the Gamma function from the math package.
  • The function factorial takes a float64 n as input and returns the factorial of that number as a float64.
  • It uses the Gamma function to compute the factorial of n, which is defined as the product of all the positive integers less than or equal to n.

4- The fourth code snippet:

  • The code snippet implements the factorial function using the Lgamma function from the math package and the SetMantExp method of the big package.
  • The function lfactorial takes a float64 n as input and returns the natural logarithm of the factorial of that number as a float64.
  • The function factorial takes a float64 n as input and returns the factorial of that number as a big.Float.
  • It uses the Lgamma function to compute the natural logarithm of the factorial of n and then uses the SetMantExp method to convert the result to a big.Float.

Source code in the go programming language

package main

import (
    "fmt"
    "math/big"
)

func main() {
    fmt.Println(factorial(800))
}

func factorial(n int64) *big.Int {
    if n < 0 {
        return nil
    }
    r := big.NewInt(1)
    var f big.Int
    for i := int64(2); i <= n; i++ {
        r.Mul(r, f.SetInt64(i))
    }
    return r
}

package main

import (
    "math/big"
    "fmt"
)

func factorial(n int64) *big.Int {
    var z big.Int
    return z.MulRange(1, n)
}

func main() {
    fmt.Println(factorial(800))
}

package main

import (
    "fmt"
    "math"
)

func factorial(n float64) float64 {
    return math.Gamma(n + 1)
}

func main() {
    for i := 0.; i <= 10; i++ {
        fmt.Println(i, factorial(i))
    }
    fmt.Println(100, factorial(100))
}

package main

import (
    "fmt"
    "math"
    "math/big"
)

func lfactorial(n float64) float64 {
    l, _ := math.Lgamma(n + 1)
    return l
}

func factorial(n float64) *big.Float {
    i, frac := math.Modf(lfactorial(n) * math.Log2E)
    z := big.NewFloat(math.Exp2(frac))
    return z.SetMantExp(z, int(i))
}

func main() {
    for i := 0.; i <= 10; i++ {
        fmt.Println(i, factorial(i))
    }
    fmt.Println(100, factorial(100))
    fmt.Println(800, factorial(800))
}

  

You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Maple programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Oforth programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Factor programming language
You may also check:How to resolve the algorithm Benford's law step by step in the F# programming language