Big Data factorial Algorithm
Reprinted please indicate the source: http://blog.csdn.net/ZhouLi_CSDN/article/details/46316855
I haven't written an algorithm for a long time. Today I suddenly want to write a big number factorial operation. I spent more than an hour writing a demo. I didn't think about the time space complexity optimization of the specific algorithm. If you are interested, please study it yourself.
No additional code is required:
Public class BigNumber {public static void main (String [] args) {// factorial factor int jie = 8000; int [] num1 = new int [5]; int [] num2 = new int [30000]; int [] result = new int [30000]; int length1 = num1.length; int lengh2 = num2.length; int length3 = result. length; // int g = 0; num1 [0] = 1; num2 [0] = 1; for (int I = 1; I <jie; I ++) {// initialize num1 12345 num1 [0] = I % 10; num1 [1] = (I/10) % 10; num1 [2] = (I /100) % 10; num1 [3] = (I/1000) % 10; num1 [4] = (I/10000) % 10; // g indicates carry int g = 0; for (int j = 0; j <length1; j ++) {// num1 for (int m = 0; m <leng22; m ++) {// num2 if (m + j + 1)> 29999) {break ;} int r = num1 [j] * num2 [m] + result [m + j] + g; g = 0; if (r> 9) {result [m + j] = r % 10; g = r/10;} else {result [m + j] = r ;}}} // convert the computing result once for (int k = 0; k <leng2; k ++) {num 2 [k] = 0; num2 [k] = result [k]; result [k] = 0 ;}// output result int flag = 0; int fuck = 0; for (int k = 29999; k> = 0; k --) {if (num2 [k]! = 0) {flag = 1; fuck ++; System. out. print (num2 [k]);} else {if (flag = 1) {fuck ++; System. out. print (num2 [k]) ;}} if (fuck % 60 = 0 & fuck! = 0) {System. out. println ("\ n") ;}} System. out. println ("\ n" + fuck + "");}}
The comment is clearly written. You can try to run the test result:
Inspection site: http://zh.numberempire.com/factorialcalculator.php