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

Published on 12 May 2024 09:40 PM
#Go

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

Table of Contents

Problem Statement

The factorial of a number, written as

n !

{\displaystyle n!}

, is defined as

n !

n ( n − 1 ) ( n − 2 ) . . . ( 2 ) ( 1 )

{\displaystyle n!=n(n-1)(n-2)...(2)(1)}

. Multifactorials generalize factorials as follows: In all cases, the terms in the products are positive integers. If we define the degree of the multifactorial as the difference in successive terms that are multiplied together for a multifactorial (the number of exclamation marks), then the task is twofold:

Note: The wikipedia entry on multifactorials gives a different formula. This task uses the Wolfram mathworld definition.

Let's start with the solution:

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

This is a program written in the Go programming language that computes the multifactorial of a number.

The multifactorial of a number n and k is defined as n * (n - k) * (n - 2k) * ..., continuing until reaching 1.

The multiFactorial function takes two integers, n and k, and returns the multifactorial of n and k.

The main function iterates over k from 1 to 5, and for each value of k it iterates over n from 1 to 10, printing the multifactorial of n and k.

The output of the program is:

degree 1: 1 2 3 4 5 6 7 8 9 10
degree 2: 1 1 2 6 24 120 720 5040 40320 362880
degree 3: 1 1 1 1 2 12 120 1680 30240 665280
degree 4: 1 1 1 1 1 1 1 2 20 384
degree 5: 1 1 1 1 1 1 1 1 1 1

Source code in the go programming language

package main

import "fmt"

func multiFactorial(n, k int) int {
    r := 1
    for ; n > 1; n -= k {
        r *= n
    }
    return r
}

func main() {
    for k := 1; k <= 5; k++ {
        fmt.Print("degree ", k, ":")
        for n := 1; n <= 10; n++ {
            fmt.Print(" ", multiFactorial(n, k))
        }
        fmt.Println()
    }
}


  

You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the TXR programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the Julia programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Factor programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Ada programming language