# Need help with using GMP BigNum Library



## FilledVoid (Mar 19, 2008)

Hi all,
 I have been working on Project Euler questions and I'm trying to understand how to use this GMP Bignum library and I've read through a bit of the documentation but this is way way more complex than what I expected lol. I'd appreciate if soeone could either post a few *SIMPLE* examples using it. If you could attach the code, that woulds be great. Please dont post the ones in the demo folder of the gmp library cause I can barely understand them at all lol.
Thanks


----------



## QwertyManiac (Mar 19, 2008)

Here's a simple code which adds and subtracts 2 integers among 3:


```
#include <gmp.h>

int main(void)
{
    // We declare a few BigNum Integer
    mpz_t I, K, M;
    
    // Some BigNum Floats (Not gonna use them)
    // Just demo
    mpf_t U,J,N;
    
    int n;
    
    // Initialize the Integer I as 0
    // set a value for the rest two (K and M)
    
    // We need to initialize ANY BigNum value
    // Its a must
    
    mpz_init(I); // Initialized with 0
    
    mpz_init(K);
    
    mpz_set_si(K, -3000000);
    // si means Signed Integer
    // set_* is used to set values
    
    mpz_init_set_ui(M, 999999999);
    // Initialized and set via Unsigned at a time
    
    // Similarly one can convert via Double, Strings, etc
    // and even set via other mpz_t values
    // Also use xxx_clear() to clear
    
    // Add M, K and store in I
    mpz_add(I,M,K);
    
    // Subtraction, another example
    mpz_sub(M,K,I);
    
    gmp_printf("I = %Zd, K = %Zd, M = %Zd \n", I, K, M);
    
    // Refer the manual for other _functions_ relating to integers
    
    return 0;
}

/*

Note on GMP in general

There are 3 basic types of Numbers in GMP
    Integer - Represented as "Z"
    Float - Represented as "F"
    Rational - Represented as "Q"

There are some more types, such as Limbs and so,
but I don't know much about them.

So in gmp_printf(), the strings obviously are:
%Z<format> for Integer
%F<format> for Float
%Q<format> for Rational

Where <format> can be any C-allowed printf() type such as:
d - Integer
f - Float
x - Hex

gmp_scanf()'s the same

P.s. Heard that stuff for O/P and I/P are easier in C++ but,
I prefer C
*/
```

See it in some editor which highlights comments out at least.


----------



## Sykora (Mar 19, 2008)

Are you sure you checked the manual? It seemed pretty simple to me. Just read the part on initialization, then all the arithmetic functions, then the I/O. That should be all you need.


----------



## ChaiTan3 (Mar 19, 2008)

Even I had decided to use BigNum library for some of ProjectEuler's problems. But due to its complexity, learning python turned out to be a simpler option(learning  loops and functions hardly takes 10 min)


----------



## FilledVoid (Mar 19, 2008)

QwertM : Thank you for the guidance  That explains alot. 



> Are you sure you checked the manual? It seemed pretty simple to me. Just read the part on initialization, then all the arithmetic functions, then the I/O. That should be all you need.



Hmm Maybe Im looking at the wrng manual then.  But I felt that it was way too complex  . 



> Even I had decided to use BigNum library for some of ProjectEuler's problems. But due to its complexity, learning python turned out to be a simpler option(learning loops and functions hardly takes 10 min)



yeah I've beent empted to change to Python but I'd like to make sure I atleast try in C befoer I give up.


----------



## QwertyManiac (Mar 20, 2008)

This is the right manual:

*gmplib.org/manual/index.html


----------



## FilledVoid (Mar 20, 2008)

> This is the right manual:
> 
> *gmplib.org/manual/index.html


Thank you


----------



## Sykora (Mar 20, 2008)

The entire python standard library _is_ written in C. So if you maximize your usage of builtins, you'll be able to get C's speed, and write in python, which is heavenly.


----------

