One, the introduction

GMP(The GNU Multiple Precision Arithmetic Library), also known as GNU Multiple Precision Arithmetic Library, is an Arithmetic Library that provides many high-precision operations of large integers and floating point numbers, with almost no restrictions on accuracy and rich functions. I first got into this stuff when I was learning PHP. The main target application areas of GMP are the application and research of cryptography, Internet security applications, algebraic systems, computational algebra and so on.

Two, usage introduction

Detailed usage of GMP can be found in the official manual: gmplib.org/gmp-man-6.1… , which introduces the use of GMP in detail. Here’s a quick summary of the basic usage:

Add header file: #include <gmp.h>

GCC myprogram.c -lgmp-lm -o myprogram

Declare a GMP integer ROP: mpz_t rop;

Int mpz_init_set_str(mpz_t rop, char* STR, int base);

Void mpz_clear(mpz_t rop); // Declare a variable, must be released at the end of the program, otherwise an error will be reported

Add: void mpz_add(mpz_t rop, mpz_t op1, mpz_t op2); //rop = op1 + op2

Void mpz_sub(mpz_t rop, mpz_t op1, mpz_t op2); //rop = op1 – op2

Multiply: void mpz_mul(mpz_t rop, mpz_t op1, mpz_t op2); //rop = op1 * op2

Void mpz_cdiv_q (mpz_t q, mpz_t n, mpz_t d); //q = n/d

Void mpz_pow_ui (mpz_t rop, mpz_t base, unsigned long int exp); //rop = base^exp

Void mpz_sqrt (mpz_t rop, mpz_t op); //rop = the square root of op

.

The above is the basic use of GMP, detailed or see the use of the manual ~~

3. Install and configure GMP in Linux/Windows

1. Install and configure GMP on Linux

First go to the official website to download GMP-6.1.2.tar. lz, the official website link is gmplib.org/

Decompress the downloaded installation package and enter tar -jxvf gmp-6.1.2.tar.lz

/configure — enable-cxx make make check sudo make install./configure — enable-cxx

Then you can use it

2. Install and configure GMP on Windows

The first step is to install MinGW, which is a Windows version of GCC and a collection of useful GNU tools that generate native Windows programs without the need for third-party C Runtime libraries. There are many things you can install from minGW, such as the GCC and g++ compilers and the GMP large number library.

In the official website to download MinGW installation package: sourceforge.net/projects/mi…

  

Click “Install”, then remember the installation path in the next step, default: C:\MinGW

  

  

Click continue all the way and wait for the installation (be sure your network is working). When the installation is complete, the following window will pop up:

  

You can then install it from here, but you can also configure the environment variables first and open it directly in CMD. (Mingw-get-exe can be opened by clicking mingw-get-exe in the bin folder of the installation path)

To configure environment variables: Open the Control Panel -> System and Security – System, click “Advanced System Settings” on the left, then click Environment Variables or directly from my computer -> Properties -> Advanced System Settings -> Environment Variables:

Find the Path of the system environment variable, click “Edit”, “New”, and add the installation Path (my system is Windows 10, if it is Windows 7 remember to add a semicolon before adding) :

You can then open the mingw32-base on the command line, right-click “Mark for Installation”, and then click “Apply Changes” in the upper left corner of the window, and click “Apply”. Then click Close after installing:

Mingw32-gcc-g ++ and mingw32-GMP can be installed in the same way.

You can also install it from the command line, open CMD, and type the following command:

mingw-get install mingw32-base\

mingw-get install mingw32-gcc-g++\

mingw-get install mingw32-gmp\

Then from the command line, type GCC, or GCC -v. If the following information appears, the configuration is successful.

Iv. Case study

Below we seek 10000! Give an example of how to use GMP. To use GMP, you must first include the GMP header:

#include <gmp.h>
Copy the code

For 10000! Multiple precision integer multiple precision integer Multiple precision integer Multiple precision integer multiple precision integer

mpz_t num;
Copy the code

Now we need to define three variables:

mpz_t z_i, z_s, z_o;
Copy the code

Respectively used to iterate 1.. The number between 10000, save the result, save the number 1 to make z_i increment. A string can be used to initialize multiple precision numbers to a large number:

mpz_init_set_str(z_i, "1", 10);
mpz_init_set_str(z_s, "1", 10);
mpz_init_set_str(z_o, "1", 10);
Copy the code

The prototype for mpz_init_set_str is:

int mpz_init_set_str (mpz_t rop, char *str, int base)

These three parameters are multiprecision integer variables, strings, and bases.

Now we loop 10,000 times and multiply and add. The functions of multiply and add are mpz_mul and mpz_add respectively. The prototypes are:

void mpz_add (mpz_t rop, mpz t op1, mpz t op2)

Rop = OP1 + OP2

void mpz_mul (mpz_t rop, mpz t op1, mpz t op2)

The result is rop = OP1 * OP2

Our program can be written as:

int i;
for (i = 0; i < 10000; i++)
{
   mpz_mul(z_s, z_s, z_i);
   mpz_add(z_i, z_i, z_o);
}
Copy the code

We then print the result as a large integer. Since it is mpz_t, we cannot use printf, only gmp_printf:

gmp_printf("%Zd\n", z_s);
Copy the code

Finally, we free up the space taken up by these large integers:

mpz_clear(z_i);
mpz_clear(z_s);
mpz_clear(z_o);
Copy the code

The procedure is complete. It’s a very large result, it’s a couple of pages but it’s very fast, it’s done in less than a second and it includes printing time on the console.

The complete procedure is as follows:

1 #include <gmp.h> 2 #include <string.h> 3 int main(int argc, const char *argv[]) 4 { 5 mpz_t z_i, z_s, z_o; 6 mpz_init_set_str(z_i, "1", 10); 7 mpz_init_set_str(z_s, "1", 10); 8 mpz_init_set_str(z_o, "1", 10); 9 int i; 10 for (i = 0; i < 10000; i++) 11 { 12 mpz_mul(z_s, z_s, z_i); 13 mpz_add(z_i, z_i, z_o); 14 } 15 gmp_printf("%Zd\n", z_s); 16 mpz_clear(z_i); 17 mpz_clear(z_s); 18 mpz_clear(z_o); 19 getchar(); 20 return 0; 21}Copy the code

GCC test.cpp -lgmp-lm -o test GCC test.cpp -lgmp-lm -o test GCC test.cpp -lgmp-lm -o test

The current folder automatically generates a test.exe file

Click test.exe to run it, and you can see the result as follows:

Due to the large number of pages, I will not take all the screenshots, to calculate such a large number, the computer compiled and ran the print results took less than 1s, enough to reflect the GMP function library powerful ~~~

In-depth understanding of GMP or have a look at the official manual gmplib.org/gmp-man-6.1…