tags:

views:

59

answers:

2

While i m using gmp.h header file. i need a fuction which takes inputs of type mpz_t and return mpz_t type too. I m very beginner of using gmp.h So, Here is snaps follows of my approached code....

mpz_t sum_upto(mpz_t max)  
{    
    mpz_t sum;  
    mpz_init(sum);    
    mpz_init(result);  
    for(int i=0;i<=max-1;i++)    
        mpz_add_ui(sum,sum,pow(2,i));   
    return sum;   
}

but it will show error
1 ." pow has been not used in this scope.",although i have added math.h in very beginnig of the file.
2 . sum_upto declared as function returning an array...

do tell as soon as possible. i have to complete my very big project....

A: 

Try the following:

mpz_t sum_upto(mpz_t max)
{
    mpz_t sum;
    mpz_init(sum);
    mpz_init(result);
    int val = 1;
    for(int i=0;i<=max-1;i++) {
        mpz_add_ui(sum,sum,val);
        val *= 2; //compiler should make a shift operation out of it
    }
    return sum;
}

Furthermore you can possibly drop the math.h header.

ablaeul
A: 

No my dear, it is not done. You are saying that i should change my aim. I have problem with returning of the function.Power problem has been resolved but i could not able to resolve return type problem. If u can help on it then plz. By the way, you should know about GMP.h and their functionality first. If u knw then well otherwise first go through GMP.h, then after if u can help me. THnks in advance my dear

Raveesh_Kumar