简体   繁体   English

Fibonacci数字与GMP lib

[英]Fibonacci numbers with GMP lib

EDIT: solved. 编辑:解决了。 ans var needed to be set to 0 in every iteration.That was a mistake I overlooked.A mpz_set_ui(ans,0); ans var需要在每次迭代中设置为0.这是我忽略的一个错误。一个mpz_set_ui(ans,0); at the beginning of each loop solves the problem.Thanks to Oli Charlesworth for his effort! 在每个循环开始时解决了这个问题。感谢Oli Charlesworth的努力!

I'm coding a fibonacci function in C using the GMP library,I use the matrix multiplication algorithm,but for some unexpected reason,I get wrong results(I strongly believe that the algorithm is right).It must have something to do with Gmp structs and inits.Any ideas? 我使用GMP库在C中编码斐波那契函数,我使用矩阵乘法算法,但出于某些意想不到的原因,我得到了错误的结果(我坚信算法是对的)。它必须与Gmp有关结构和inits.Any想法?

/*********************************************************
Find n th Fibonacci number.Matrix algorithm.
+--          --+     +--     --+
|f(n+1) f(n)   |     | 1     1 |  ^n
|              |  == |         |
|f(n)   f(n-1) |     | 1     0 |
+--          --+     +--     --+
*********************************************************/

int
fastfib(mpz_t result,int n)
{


    int cons[2][2]={{1,1},{1,0}};      //"constant" matrix
    mpz_t mat[2][2];                   //actual matrix holding fibonacci numbers
    mpz_t ans;                         //holds f(n+1)   
    mpz_t return_val;                  //used for calculations
    mpz_t temp;                        //used for calculations as well

    //initialize (automatically set to zero)
    mpz_init(ans);
    mpz_init(return_val);
    mpz_init(temp);
    mpz_init(mat[0][0]);
    mpz_init(mat[0][1]);
    mpz_init(mat[1][0]);
    mpz_init(mat[1][1]);

    //start with n=1
    mpz_set_ui(mat[0][0],1);
    mpz_set_ui(mat[1][0],1);
    mpz_set_ui(mat[0][1],1);
    mpz_set_ui(mat[1][1],0);

    //some trivial cases
    if(n==0){
        mpz_set_ui(result,0);
        return 0;
    }
    if(n==1){
        mpz_set_ui(result,1);
        return 0;
    }
    if(n==2){
        mpz_set_ui(result,1);
        return 0;
    }

    n--;


    while(n>1){

        //fib[n+1]
        //ans=mat[0][0]*cons[0][0]+mat[0][1]*cons[1][0];

        mpz_set_ui(ans,0);

        mpz_mul_ui(temp,mat[0][0],cons[0][0]);
        mpz_add(ans,ans,temp);
        mpz_mul_ui(temp,mat[0][1],cons[1][0]);
        mpz_add(ans,ans,temp);

        //update matrix


        mpz_set(mat[1][1],mat[1][0]);   //mat[1][1]=mat[1][0];
        mpz_set(mat[1][0],mat[0][0]);   //mat[0][1]=mat[1][0]=mat[0][0];
        mpz_set(mat[0][1],mat[0][0]);
        mpz_set(mat[0][0],ans);         //mat[0][0]=ans;

        n--;
    }


    //clear vars
    mpz_clear(ans);
    mpz_clear(return_val);
    mpz_clear(temp);

    mpz_set(result,mat[0][0]);
    return 0;

}

Some debug info 一些调试信息

results for n=2       // expected
2 1                   //  2 1
1 1                   //  1 1

results for n=3
5 2                   //  3 2
2 1                   //  2 1

results for n=4
12 5                  //  5 3
5 2                   //  3 2

results for n=5
29 12                 //  8 5
12 5                  //  5 3

results for n=6
70 29                 //  13 8
29 12                 //  8  5

70

solved. 解决了。 ans var needed to be set to 0 in every iteration.That was a mistake I overlooked.A mpz_set_ui(ans,0); ans var需要在每次迭代中设置为0.这是我忽略的一个错误。一个mpz_set_ui(ans,0); at the beginning of each loop solves the problem.Thanks to Oli Charlesworth for his effort! 在每个循环开始时解决了这个问题。感谢Oli Charlesworth的努力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM