Use an array to calculate the 256 factorial

Source: Internet
Author: User

What if we want to accurately calculate a large number, such as a 256 factorial (with more than 500 digits?

You will say, it's easy to do. Since JDK 1.1, Java does not provide {
Tagshow (Event)
} "> JAVA. Math. biginteger? Yes. Using biginteger can solve the problem. However, without the class given by Sun, we only rely on the most basic types of Java. Is there a way to perform computation? The answer is: yes. What else should I do before biginteger.

One of the methods is to use {
Tagshow (Event)
} "> Array. For example:
Int [] DATA = new int [100];

We know that the maximum value of an int is 2 ^ 31-1, that is, 2147483647 (10 bits). If we concatenate these 100 int values, we can represent a 1000 bits. Here we use this method to calculate the 256 factorial (256 !).
First, we allocate an array of 100 int values. Because it is static, the initial value of each int value is 0.

Each int represents six digits, that is, the maximum value is 999999. Because we need to perform multiplication. If the number of digits given to the int is too large, such as 9 digits, then the value of 999999999 multiplied by the previous number, such as 100, is greater than the max value of the int, resulting in overflow. Therefore, the number of digits indicated by INT must be carefully selected as needed. (The number of digits must also be carefully weighed if long is used)

Define another num to indicate the number of int values in the occupied array.
During multiplication, the number in each occupied int must be multiplied, and then the value of each int must be judged one by one if it exceeds 6 digits:
If (data [J])> 1000000)
If this parameter is exceeded, carry is required:
Data [k + 1] + = data [k]/1000000;
Data [k] % = 1000000;
One by one judgment. Finally, if the value in the highest bit (that is, data [num]) exceeds six digits, we need to take up a new int and carry it similarly, of course, do not forget to add one to num.
If (data [num]> 1000000) num ++;
Finally, output the array order. Note that if the int value is smaller than 6 bits, for example, 25, do not forget to add 0, that is, 000025. Otherwise, you will get the wrong answer.
Complete {
Tagshow (Event)
} "> The code is as follows:

Package TMP; </P> <p>/** <br/> * @ author stevech <br/> */<br/> public class bignumbers {<br/> static int [] DATA = new int [100]; </P> <p>/** creates a new instance of bignumers */<br/> Public static void main (string [] ARGs) {<br/> int num = 0; // number of occupied items <br/> data [0] = 1; // The factorial of 0 and 1 is 1 </P> <p> for (INT I = 2; I <257; I ++) {<br/> for (Int J = 0; j <num + 1; j ++) {<br/> data [J] * = I; // multiply the number in each int by I <br/>}< br/> for (Int J = 0; j <num + 1; j ++) {<br/> If (data [J]> 1000000) {<br/> for (int K = J; k <num + 1; k ++) {<br/> If (data [num]> 1000000) num ++; <br/> data [k + 1] + = data [k]/1000000; // carry <br/> data [k] %= 1000000; // The remainder after carry <br/>}< br/> system. out. println ("Number of int used:" + (Num + 1) + "/n value:"); <br/> system. out. print (data [num]); <br/> for (INT I = num-1; I>-1; I --) {<br/> system. out. print (New Java. text. decimalformat ("000000 "). format (data [I]); <br/>}< br/>}

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.