C++ factorial, primorial

Marc Glisse marc.glisse at inria.fr
Fri Jun 20 07:05:34 UTC 2014


Hello,

I am trying to make a few more functions easily available from the C++ 
interface. mpz_fac_ui takes an unsigned long, so it would be natural to 
have a factorial function that takes any of {signed,unsigned} 
{char,short,int,long}. It would probably throw an out_of_range error for 
negative values. But since it does not take a gmp type as argument, I 
should not make it a free function in the global namespace. The easiest 
seems to be a static member function, so we would call:

mpz_class::factorial(42);

If we have an mpz_class that represents a small enough number, we would 
probably like to be able to use factorial on it directly without 
explicitly extracting an unsigned long. We could have 
mpz_class::factorial(z); but since there is a GMP argument, this versions 
could also be available as simply factorial(z);. Because of the way it 
would be done with templates, factorial(42) would not compile (it would 
not automatically convert 42 to mpz_class), which is good. However, I 
think we could do without this global factorial for now, and it can be 
added later if people want it.

Are there any comments?

-- 
Marc Glisse


More information about the gmp-discuss mailing list