Java does not use BigInteger to implement Big Data multiplication. javabiginteger

Source: Internet
Author: User

Java does not use BigInteger to implement Big Data multiplication. javabiginteger
I saw a question yesterday: Calculate 1234 !, The BigInteger class cannot be used.
As we all know, the factorial data is very large, and the commonly used int and long types are not enough. Generally, only the BigInteger class is expected, but the question clearly states that it cannot be used, so you can only find other methods.
Factorial is actually the recursion of multiplication. This question can be simplified to how to implement Big Data multiplication. int and long data cannot be loaded, and can only be represented by String, therefore, we only need to implement the multiplication of two strings to represent numbers.
Think about the procedure of manually calculating the multiplication, which is basically a vertical column, multiplying by bit, and adding the numbers of carry .. As long as the process is simulated by a program, it is OK.

When the column is vertical, a number is divided into hundreds of thousands .. In fact, it is equivalent to an integer array .. After understanding this, you can write the code.

Package cn. baokx; public class Training {public static int [] multi (String str1, String str2) {// convert the received string into an inverted char array StringBuffer buffer = new StringBuffer (); buffer. append (str1); char [] nums1 = buffer. reverse (). toString (). toCharArray (); buffer. setLength (0); buffer. append (str2); char [] nums2 = buffer. reverse (). toString (). toCharArray (); // declare an array in advance to store the result of multiplying individual digits (similar to a Column Vertical) int len = nums1.length + nums2.length; int [] Array = new int [len]; // simulate vertical Calculation for (int I = 0; I <nums2.length; I ++) {for (int j = 0; j <nums1.length; j ++) {array [len-1-(I + j)] + = (nums2 [I]-48) * (nums1 [j]-48) ;}return array ;}// perform carry operations on the array, returns the final result public static String arrayFormat (int [] array) {for (int I = array. length-1; I> 0; I --) {array [I-1] + = array [I]/10; array [I] = array [I] % 10 ;} stringBuffer buffer = new StringBuffer (); if (array [0]! = 0) {buffer. append (array [0]);} for (int I = 1; I <array. length; I ++) {buffer. append (array [I]);} return buffer. toString ();} // factorial public static String getFactorial (String num) {if ("1 ". equals (num) {return "1";} else {return arrayFormat (multi (num, getFactorial (Integer. parseInt (num)-1) + "");} public static void main (String [] args) {System. out. println (getFactorial ("1234"); // System. out. println (arrayFormat (multi ("10", "10"); // System. out. println (arrayFormat (multi ("99", "99 ")));}}



JAVA Big Data factorial, how to do it without using biginteger

Low string Efficiency
Array is much more efficient
The following is a list of the 1000-item Fibonaci series. For more information, see change to factorial.

Public class Fibonacci
{
Private static final int MAX_LENGTH = 1000000;
Private static int [];
Private static int [] B;
Private final static int LEN = 1000000000;
Private static int carry = 1;

Public static void main (String [] args)
{
A = new int [MAX_LENGTH];
B = new int [MAX_LENGTH];
A [0] = 1;
B [0] = 1;

Double begin = System. currentTimeMillis ();
For (int I = 0; I <1000; I ++)
{
AddToA ();
AddToB ();
}
Display (B );
Double end = System. currentTimeMillis ();
System. out. println ("Time:" + (end- begin)/1000 );
}

Public static void addToA ()
{
Int carryNum = 0;
For (int I = 0; I <carry; I ++)
{
CarryNum = (a [I] + B [I])/LEN;
A [I] = (a [I] + B [I]) % LEN;
A [I + 1] + = carryNum;

}
If (carryNum = 1)
{
Carry ++;
}
}

Public static void addToB ()
{
Int carryNum = 0;
For (int I = 0; I <carry; I ++)
{
CarryNum = (B [I] + a [I])/LEN;
B [I] = (B [I] + a [I]) % LEN;
B [I + 1] + = carryNum;

}
If (carryNum = 1)
{
Carry ++;
}
}

Public static void display (int [] n)
{
For (int I = carry-1; I> = 0; I --)
{
System. out. print (n [I]);
}
System. out. println ();
}
}... Remaining full text>

Number of JAVA combinations, without BigInteger

Write high-precision operations in java ~~ Similar to that written in C.

Open two arrays first, and each array element stores a large integer.
For example, 456687855 is

0 1 2 3 4 5 6 7 8 9
5 5 8 7 8 6 6 5 4

The + method is to add two arrays by bit and then carry them.

Subtraction is to subtract by bit and then borrow

Multiplication is to take the number of the second array by bit and multiply by an array.
Then accumulate the multiplication to things.

Division is just how you write programs.

C (1000,500) cannot be calculated directly.
Otherwise, it will take to and to time out (1 hour may not be counted)
You store all the prime numbers of 100000 in the array.
Multiplication means to add all the factors to the variables of each prime number. Subtract when Division

For example, the prime number series

0 1 2 3 4 5 6 7 8 9 // array bit
2 3 5 7 11 13 17 19 23 29 // Prime Number
10 8 3 2 0 0 0 0 0 0 // multiply the number of prime numbers in this large number
Now the big number is * 210
It becomes
11 9 4 3 0 0 0 0 0 0
In addition, 2 is
10 9 4 3 0 0 0 0 0

Finally, calculate 2 ^ n2 + 3 ^ n3 + .................. Just do

In fact, there is a faster method. This is the scope of the number theory discussion.
Think about how to improve efficiency.
But the idea is clear.

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.