Algorithm gossip: ultra-long integer computing (big data computing)

Source: Internet
Author: User
Description

Based on the effective use of the memory, the program statement defines different types of data types. As a result, the maximum integer of data tables that can be changed is limited, for example, an integer like 123456789123456789 cannot exist in the long variable (for example, C/C ++). We assume this is the long data, this can be converted to an ultra-long integer (to avoid confusion with the full-length index of the resource type), or a massive data calculation.
Solution

If a variable cannot represent an integer over a long value, multiple variables are used. Of course, using the variable column is the most convenient, the maximum data type of the hypothetical program statement can be saved to 65535, so that the computation is convenient and the computation conforms to the ten-step system, each element in each column can store four digits, that is, the numbers from 0 to 9999. For example:

Many people ask how to calculate 50! In this case, the solution is to use the multiplication function in the program. It depends on the requirement as to how big it is.

If you are using Java, biginteger and bigdecimal in Java. Lang can be directly used for big data computing.

Used to store data in the values column, it is necessary to define the progress or borrow of various types of operations such as addition, multiplication, division, and number during operation, addition, multiplication, and multiplication start from low-level data, while division starts from high-level data, in this example, the formula for the addition, multiplication, and Division operations is provided for the test. The N below is the maximum length.

Implementation
  • C
Void add (int * a, int * B, int * c ){
Int I, carry = 0;

For (I = n-1; I> = 0; I --){
C [I] = A [I] + B [I] + carry;
If (C [I] <10000)
Carry = 0;
Else {// advance
C [I] = C [I]-10000;
Carry = 1;
}
}
}

Void sub (int * a, int * B, int * c ){
Int I, borrow = 0;

For (I = n-1; I> = 0; I --){
C [I] = A [I]-B [I]-Borrow;
If (C [I]> = 0)
Borrow = 0;
Else {// borrow
C [I] = C [I] + 10000;
Borrow = 1;
}
}
}

Void MUL (int * a, int B, int * c) {// B is the multiplication number
Int I, TMP, carry = 0;

For (I = n-1; I> = 0; I --){
TMP = A [I] * B + carry;
C [I] = tmp% 10000;
Carry = tmp/10000;
}
}

Void Div (int * a, int B, int * c) {// B is the division number
Int I, TMP, remain = 0;

For (I = 0; I <n; I ++ ){
TMP = A [I] + remain;
C [I] = tmp/B;
Remain = (TMP % B) * 10000;
}
}

  • Java
Public class bignumber {
Public static int [] add (INT [] A, int [] B ){
Int carry = 0;
Int [] C = new int [A. Length];

For (INT I = A. Length-1; I> = 0; I --){
C [I] = A [I] + B [I] + carry;
If (C [I] <10000)
Carry = 0;
Else {// advance
C [I] = C [I]-10000;
Carry = 1;
}
}

Return C;
}

Public static int [] sub (INT [] A, int [] B ){
Int borrow = 0;
Int [] C = new int [A. Length];

For (INT I = A. Length-1; I> = 0; I --){
C [I] = A [I]-B [I]-Borrow;
If (C [I]> = 0)
Borrow = 0;
Else {// borrow
C [I] = C [I] + 10000;
Borrow = 1;
}
}

Return C;
}

Public static int [] Mul (INT [] A, int B) {// B is a multiplier
Int carry = 0;
Int [] C = new int [A. Length];

For (INT I = A. Length-1; I> = 0; I --){
Int TMP = A [I] * B + carry;
C [I] = tmp% 10000;
Carry = tmp/10000;
}

Return C;
}

Public static int [] Div (INT [] A, int B) {// B is the division number
Int remain = 0;
Int [] C = new int [A. Length];

For (INT I = 0; I <A. length; I ++ ){
Int TMP = A [I] + remain;
C [I] = tmp/B;
Remain = (TMP % B) * 10000;
}

Return C;
}

Public static void main (string [] ARGs ){
Int [] A = {1234,567 8, 9910,192 3, 1124 };
Int [] B = {1234,567 8, 9910,192 3, 1124 };
Int [] C = bignumber. Add (A, B );

For (INT I = 0; I <C. length; I ++ ){
System. Out. Print (C [I]);
}
System. Out. println ();
}
}

 

Related Article

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.