Installation and debugging of open-source Algorithm Library GMP

Source: Internet
Author: User
Tags gmp

GMP introduction:
GMP is an open-source arithmetic library with any precision. It can be used for symbol integers, rational numbers, and floating point numbers. Does the arithmetic library have actual restrictions? The only limitation is the computer memory. GMP has rich function sets and functions all have common interfaces.

Installation of GMP:

Environment: ubuntu11.10

Run in terminal:
Sudo apt-Get install libgmp3-dev
GMP debugging:
Create a. c file and enterCode.
# Include <GMP. h>
# Include <stdio. h>
Void main ()
{
Mpz_t S, N;
Int I;

Mpz_init (s); // init S, the value is 0
Mpz_init (N );

For (I = 1; I <1111111111; I ++)
{
Mpz_add_ui (N, N, 1); // set N to n + 1
Mpz_addmul (S, N, N); // Add N * n to S
}

Gmp_printf ("the sum is % ZD \ n", S );

Mpz_clear (s );
}
Compile in termial:
GCC test. C-o Test-lgmp
Run in termial:
./Test
It takes about 3 minutes. The result is as follows:
The sum is 457247370073159579654778235

Function encapsulation:
The purpose of encapsulation is to provide a common interface. The following is the multiplication of large numbers.
# Include <GMP. h>
# Include <stdio. h>
Char * bigmul (char * m, char * n );
Void main ()
{
Char * P = NULL;
Char * A = "12345678 ";
Char * B = "23456789 ";
P = bigmul (A, B );
Printf ("the result is % S./N", P );
}
Char * bigmul (char * m, char * n)
{
Int I, J;
Char * PT = NULL;
Mpz_t S, P, Q;
Mpz_init (s );
I = mpz_init_set_str (p, M, 10); // get number from m
J = mpz_init_set_str (Q, N, 10 );
// Printf ("I, J: % d, % d \ n", I, j );
Gmp_printf ("% ZD \ n", p, q );
Mpz_addmul (S, P, Q); // calculate result
// Gmp_printf ("the result is % ZD \ n", S );
PT = mpz_get_str (PT, 10, S); // get string from S
// Printf ("% s \ n", pt );
Mpz_clear (s );
Return pt;
}
Two string variables are used to transmit the data and then return the result pointer.
Calculation Result:
The result is 289589963907942.
The next step is to build a plug-in on the sipesc mechanics platform.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.