Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Super-large Integer factorial algorithm in Java

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you the content of an example analysis of super-large integer factorial algorithms in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

This method uses the "array carry" algorithm. In the case of exceeding the value range of computer variables, the multiplication of multi-digits is converted into one-digit multiplication. Such as 11! = 39916800, if the factorial of 12 is required, 39916800 needs to be multiplied by 12, and the multiplicative distribution rate can be used. The multiplication vertical is shown in the following figure:

Use an array to save the result of each bit of factorial, and an array element to hold a digit. For example, the result of factorial of 11 is 399

16800 is saved to the eight elements of the array. To calculate the factorial of 12, multiply the value of 12 by the value of each array element, and save the result to the original array element. Next, to determine whether each array element needs to carry, through the carry operation so that each element in the array has only one digit, as shown in the diagram below:

In theory, as long as the computer memory space allows, the factorial results of any number of bits can be saved, which is no longer limited by the range of variables, but only by the addressing ability of the operating system and computer memory. Tip: if you require a large factorial number, you can define the array as long to avoid overflow when calculating the product of the number of units.

The implementation code is as follows:

Public class BigInteger {/ * compute carry * @ param bit array * @ param pos is used to determine whether it is the highest bit of the array * / private void carry (int [] bit, int pos) {int I, carray = 0; for (I = 0) I = pos) / / is greater than 9 and is the highest bit {while (bit [I] > 9) / / cyclic forward carry {carray = bit [I] / 10 / / calculate the carry value bit [I] = bit [I]% 10; / / current first digit I + + Bit [I] = carray / / Save the carry value in the next bit}} / * * large integer factorial * @ large integer calculated by param bigInteger * / Private void bigFactorial (int bigInteger) {int pos = 0 / / int digit; / / data length int a, b; int m = 0; / / Statistical output digits int n = 0; / / Statistical output rows double sum = 0 / / factorial for (a = 1; a = 0; a--) / / output the result {System.out.print (fact [a]); masks + If (m% 5 = = 0) {System.out.print (") } if (40 = = m) {System.out.println (""); m = 0; n + + If (10 = = n) {System.out.print ("\ n"); n = 0 } System.out.println ("\ n" + "factorial co-ownership:" + (pos+1) + "bit");} public void doBigFactorial (int bigInteger) {int timeBegin= (int) System.currentTimeMillis () This.bigFactorial (bigInteger); int timeFinishi= (int) System.currentTimeMillis (); int time = timeFinishi-timeBegin; System.out.println ("calculation time: + time +" milliseconds ");} public static void main (String [] args) {BigInteger bi = new BigInteger () Bi.doBigFactorial (100000);}}

The factorial of 10pj0000 is calculated, and the result is shown as follows:

As a result, the console obviously can no longer save the content. The factorial of 100000 has as many as 450000 bits, which is equivalent to a novel with 450000 words. The factorial result of comparing 1000 is as follows:

The console can be fully displayed.

Thank you for reading! This is the end of this article on "example analysis of super-large integer factorial algorithms in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report