Introduction to big number calculation using GMP Library

Source: Internet
Author: User
Tags gmp

On the csdn Forum, there are a lot of problems related to large numbers of operations, which are described in a variety of ways, such as files, arrays, and polynomials. The author thinks that science should be built on the shoulders of giants as much as possible ", the GMP library is very suitable for big data operations, and its connotation is still more powerful by referencing the original document.

GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc.GMP is carefully designed to be as fast as possible, both for small operands and for huge operands. The speed is achieved by using fullwords as the basic arithmetic type, by using fast algorithms, with highly optimised assembly code for the most common inner loops for a lot of CPUs, and by a general emphasis on speed.GMP is faster than any other bignum library. The advantage for GMP increases with the operand sizes for many operations, since GMP uses asymptotically faster algorithms.The first GMP release was made in 1991. It is continually developed and maintained, with a new release about once a year.GMP is distributed under the GNU LGPL. This license makes the library free to use, share, and improve, and allows you to pass on the result. The license gives freedoms, but also sets firm restrictions on the use with non-free programs.GMP is part of the GNU project. For more information about the GNU project, please see the official GNU web site.GMP's main target platforms are Unix-type systems, such as GNU/Linux, Solaris, HP-UX, Mac OS X/Darwin, BSD, AIX, etc. It also is known to work on Windows in both 32-bit and 64-bit mode.GMP is brought to you by a team listed in the manual.GMP is carefully developed and maintained, both technically and legally. We of course inspect and test contributed code carefully, but equally importantly we make sure we have the legal right to distribute the contributions, meaning users can safely use GMP. To achieve this, we will ask contributors to sign paperwork where they allow us to distribute their work. 

It is very natural for GMP to describe the type of large numbers using strings. If a friend does need to study the operation of large numbers, reading the source code of GMP will have more substantial gains, here, the author only provides a small piece of application code to inspire others.

# Include <stdio. h> # include <GMP. h> int main (INT argc, char * argv []) {mpz_t N; If (argc <2) {printf ("Usage: gmpc1c n \ n "); return-1;}/* initialize a 10-digit big integer N and assign the first parameter of the command line to it */mpz_init (n); If (mpz_set_str (n, argv [1], 10 )! = 0) Return-1;/* print the value of N in a large integer */printf ("n ="); mpz_out_str (stdout, 10, N ); printf ("\ n");/* calculate the square of (n + 1) */mpz_add_ui (N, N, 1); mpz_mul (N, N, N ); /* print (n + 1) the square value */printf ("(n + 1) ^ 2 ="); mpz_out_str (stdout, 10, N ); printf ("\ n");/* clear and release a large integer N */mpz_clear (n); Return 0 ;}

When compiling the GMP application code, you need to link the Math Library at the same time. In the GCC environment, you can compile the Code as follows:

gcc gmpcalc.c -lgmp -lm

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.