Java Implementation 100! The factorial

Source: Internet
Author: User
Tags value store

Tag: value store style str problem new out bug ToString

Just learn the Java loop, to find the factorial problem, found that 10! You can use the int type to store the value,

What about 100? With long? Found with a long type, the result of running the output is 0, this is not true,

What is the reason? Debug with debug found, 100! Data is too large, long data storage space 8 bytes is not enough to overflow.
So what is the way to achieve 100?

The following code:

1PublicClassDemo01 {2PublicStaticvoidMain (string[] args) {3 System.out.println (factorial (100));4}56PublicStatic String factorial (Int count) { 7 BigInteger result = new BigInteger (string.valueof (1< Span style= "color: #000000"));  8 for (int i = 1; I <= count ; I++) { 9 BigInteger I_value = new BigInteger (string.valueof (i));  Ten result = result.multiply (i_value);  }12 return result.tostring (); 13 }14}     

Resolves a memory overflow problem

Java Implementation 100! The factorial

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.