diff options
Diffstat (limited to 'libk/src/math/fact.c')
-rw-r--r-- | libk/src/math/fact.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libk/src/math/fact.c b/libk/src/math/fact.c new file mode 100644 index 0000000..a146e4d --- /dev/null +++ b/libk/src/math/fact.c @@ -0,0 +1,29 @@ +#include <math.h> + +static unsigned int FACT_TABLE[] = { + 1, // 0 + 1, // 1 + 2, // 2 + 6, // 3 + 24, // 4 + 120, // 5 + 720, // 6 + 5040, // 7 + 40320, // 8 + 362880, // 9 + 3628800, // 10 + 39916800, // 11 + 479001600, // 12 +}; + +unsigned long long fact(unsigned int num) { + if (num < 13) { + return FACT_TABLE[num]; + } + unsigned long long l = FACT_TABLE[12]; + for (unsigned int i = 12; i < num;) { + i++; + l *= i; + } + return l; +} |