Programming/Kdb/Factorial

From Thalesians Wiki
< Programming‎ | Kdb
Revision as of 11:56, 30 June 2021 by Admin (talk | contribs) (Created page with "=The factorial= The factorial of a non-negative integer <math>n</math>, denoted by <math>n!</math>, is the product of all positive integers less than or equal to <math>n</mat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The factorial

The factorial of a non-negative integer , denoted by , is the product of all positive integers less than or equal to :

For example,

The value of is 1, according to the convention for an empty product.

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic use counts the number of distinct sequences—the permutations—of distinct objects: there are .

Implementing the factorial in q

We'll implement the factorial as a q function. That function will take a single parameter, x, and return the result.

We'll start with a dummy, incorrect implementation that always returns 1:

fact:{[x]1f}

This defines a function, {...}, which takes a single argument, [x], which always (irrespective of the argument) returns 1f. We assign, :, a name to that function, fact, so we can call it again and again, as required.