Endgame-Analyzer/include/factorial.h
Maximilian Keßler 5c4a2bb4f7
Store rationals without denominator
Instead of storing a rational for evey game state,
we just store how many of the factorial(draw pile size) many
game states can be won.
This allows us to save only one 64-bit integer per game state instead of
two and thus reduces memory consumption of the program significantly.
Also, this makes some computations a bit easier, since we do not have to
normalize when recursing - we can just add the numbe of winnable states
for each possible draw.

On the other hand, this means that upon lookup, we have to normalize the
stored values again to retrieve the probabilities.
In particular, one needs to know what the draw pile size of the game
state is in order to interpret the value of the state.
2024-02-09 15:58:15 +01:00

43 lines
705 B
C++

//
// Created by maximilian on 2/9/24.
//
#ifndef ENDGAME_ANALYZER_FACTORIAL_H
#define ENDGAME_ANALYZER_FACTORIAL_H
#include <cstdint>
namespace Factorial {
inline std::uint64_t factorial(std::size_t n)
{
static const std::uint64_t table[] = {
1,
1 ,
2 ,
6 ,
24 ,
120 ,
720 ,
5040 ,
40320 ,
362880 ,
3628800 ,
39916800 ,
479001600 ,
6227020800 ,
87178291200 ,
1307674368000 ,
20922789888000 ,
355687428096000 ,
6402373705728000 ,
121645100408832000 ,
2432902008176640000 ,
};
assert(n <= 20);
return table[n];
}
}
#endif //ENDGAME_ANALYZER_FACTORIAL_H