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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Multifactorial step by step in the Lua 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 Lua programming language

Source code in the lua programming language

function multiFact (n, degree)
    local fact = 1
    for i = n, 2, -degree do
        fact = fact * i
    end
    return fact
end

print("Degree\t|\tMultifactorials 1 to 10")
print(string.rep("-", 52))
for d = 1, 5 do
    io.write(" " .. d, "\t| ")
    for n = 1, 10 do
        io.write(multiFact(n, d) .. " ")
    end
    print()
end


  

You may also check:How to resolve the algorithm Ludic numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Nested function step by step in the C++ programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Maple programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the OCaml programming language