hw-horrible factorial n!. __ Note the use of big data functions BigInteger

Source: Internet
Author: User

Java.math.BigInteger Series Tutorials (iv) Reasons for the birth of BigInteger

Why is there a BigInteger type in Java? Believe that many people have this question, in fact, the reason is very simple, it can express a larger range of values, far more than the maximum value of long represents a large majority. Within an integer type, long can represent the maximum value, as follows:

1234567 public class Test {public static void main(String[] args) {System. Out. println(Long. Max_value); }}

The result is: 9223372036854775807
Using BigInteger, you can represent a larger value, theoretically, as long as you have enough memory, as in the following example:

12345678910 public class Test {public static void main(String[] args) {BigInteger a= BigInteger. ValueOf(9223372036854775807L); BigInteger b= BigInteger. ValueOf(9223372036854775807L); BigInteger C=a. Add(b); System. Out. println(C. ToString()); }}

The result is: 18446744073709551614

Because BigInteger does not overload the "+", "-", "*", "/", "%" of these five operators, it is not possible to perform data operations directly, you need to call its corresponding method: Add,subtract,multiply,divide,remainder.

The Java version is:

 Packaget0816;ImportJAva.math.BigInteger;  Public Final classBign { Public StaticBigInteger Multiply (BigInteger m,intN) {BigInteger sn=NewBigInteger (integer.tostring (n)); BigInteger sqy= M.Multiply(SN);//Big Data is subtraction with add, subtract, multiply, divide,remainder.         returnsqy; }               Public StaticString CALCNN (intN) {if(n >= 0) {BigInteger one=NewBigInteger (integer.tostring (1));//Initialization of 1             for(intI=1;i <= n;i++) { One=Multiply(One,i);//call the above-mentioned form of the accrual function;                }            returnone.tostring (); }        Else   return NULL; }          Public Static voidMain (String arg[]) {//Main functionSystem.out.println (CALCNN (13)); It is directly loaded here;}}

Output: 6227020800

hw-horrible factorial n!. __ Note the use of big data functions BigInteger

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.