相关疑难解决方法(0)

快速精确的bigint阶乘

我有一个定点bignumber库,想要实现快速阶乘,没有精度损失.

在纸上做了一些数学技巧后,我得到了这个公式:

(4N)!=((2N)!).((2N)!).{ (2N+1).(2N+3).(2N+5)...(4N-1) }.(2^N)/(N!)
Run Code Online (Sandbox Code Playgroud)

这已经非常快了,并且通过一些编程技巧,复杂性接近~ O(log(n)).

要清楚,我目前的实现是:

//---------------------------------------------------------------------------
longnum fact(const DWORD &x,longnum &h) // h return (x>>1)! to speed up computation
    {
    if (x==0) { h=1; return  1; }
    if (x==1) { h=1; return  1; }
    if (x==2) { h=1; return  2; }
    if (x==3) { h=1; return  6; }
    if (x==4) { h=2; return 24; }
    int N4,N2,N,i; longnum c,q;
    N=(x>>2);
    N2=N<<1;
    N4=N<<2;
    h=fact(N2,q);                                          // get 2N! and N!
    c=h*h; for (i=(N2+1)|1;i<=N4;i+=2) c*=i; c/=q;         // c= …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm factorial

6
推荐指数
2
解决办法
2379
查看次数

标签 统计

algorithm ×1

c++ ×1

factorial ×1