Re: Issues with factorial operator - Mailing list pgsql-hackers

From Cui Shijun
Subject Re: Issues with factorial operator
Date
Msg-id 306760850706090211r7d34f85i2f2a3e65beee86fd@mail.gmail.com
Whole thread Raw
In response to Re: Issues with factorial operator  ("Dann Corbit" <DCorbit@connx.com>)
Responses Re: Issues with factorial operator
List pgsql-hackers
2007/6/9, Dann Corbit <DCorbit@connx.com>:
> #include <math.h>
>
> double          log10nfactorialestimate(unsigned n)
> {
>     unsigned        i;
>     double          estimate = 0;
>     for (i = 1; i < n; i++)
>         estimate += log10(n);
>     return estimate;
> }
>
> #ifdef UNIT_TEST
> #include <stdio.h>
> #include <time.h>
> int             main(void)
> {
>     clock_t         start,
>                     end;
>     double          answer;
>     start = clock();
>     end = clock();
>     answer = log10nfactorialestimate(92838278);
>     printf("log 10 of 92838278! is pretty close to %g and took %g
> seconds\n",
>            answer, (end - start) / (1.0 * CLOCKS_PER_SEC));
>     return 0;
> }
> #endif
> /*
> C:\tmp>cl /W4 /Ox /DUNIT_TEST log10EST.C
> Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42
> for 80x86
> Copyright (C) Microsoft Corporation.  All rights reserved.
>
> log10EST.C
> Microsoft (R) Incremental Linker Version 8.00.50727.42
> Copyright (C) Microsoft Corporation.  All rights reserved.
>
> /out:log10EST.exe
> log10EST.obj
>
> C:\tmp>log10est
> log 10 of 92838278! is pretty close to 7.3971e+008 and took 0 seconds
> */

Hum... I think there is a little improvement: when n is too large,(say
n>10, 000) we can use Stirling's formula to get the estimated value of
n!    :-)


pgsql-hackers by date:

Previous
From: Teodor Sigaev
Date:
Subject: Re: GiST intarray rd-tree indexes using intbig
Next
From: "Dann Corbit"
Date:
Subject: Re: Issues with factorial operator