alansam

Factorial table !20

May 22nd, 2020
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uint64_t fact_u64_tabular(uint64_t val, bool * oflow) {
  2.  
  3. static
  4. uint64_t const factorials[] = {
  5. /* 0!*/ 0ULL, /* TODO: Added to make picking factorials intuitive */
  6. /* 1!*/ 1ULL,
  7. /* 2!*/ 2ULL,
  8. /* 3!*/ 6ULL,
  9. /* 4!*/ 24ULL,
  10. /* 5!*/ 120ULL,
  11. /* 6!*/ 720ULL,
  12. /* 7!*/ 5040ULL,
  13. /* 8!*/ 40320ULL,
  14. /* 9!*/ 362880ULL,
  15. /*10!*/ 3628800ULL,
  16. /*11!*/ 39916800ULL,
  17. /*12!*/ 479001600ULL, /* TODO: max factorial for 32-bit integers */
  18. /*13!*/ 6227020800ULL,
  19. /*14!*/ 87178291200ULL,
  20. /*15!*/ 1307674368000ULL,
  21. /*16!*/ 20922789888000ULL,
  22. /*17!*/ 355687428096000ULL,
  23. /*18!*/ 6402373705728000ULL,
  24. /*19!*/ 121645100408832000ULL,
  25. /*20!*/ 2432902008176640000ULL, /* TODO: max factorial for 64-bit integers */
  26. };
  27. size_t factorials_c = sizeof(factorials) / sizeof(*factorials);
  28. uint64_t fv = 0;
  29.  
  30. if (val > 0 && val < factorials_c) {
  31. fv = factorials[val];
  32. *oflow = false;
  33. }
  34. else {
  35. fv = 0;
  36. *oflow = true;
  37. }
  38.  
  39. return fv;
  40. }
RAW Paste Data