由於大數的階乘的結果比較大,長整型是不能儲存的下的,為了能實現這個功能,使用按位進行相乘,將沒位的結果分開存放,最後輸出,不過運算的速度有點慢,算2000!得好長時間,呵呵。思路如下:
24x5= 24 x 5 ------ 20 10 ------ 120
代碼如下:
import java.util.List;import java.util.ArrayList;class BigFactorial{public static void main(String args[]){BigFactorial bigFactorial = new BigFactorial();if(args.length >= 1){List<Integer> resultList = bigFactorial.computeFactorial(Integer.parseInt(args[0]));System.out.print("" + Integer.parseInt(args[0]) + "! = ");for( int i = 0; i < resultList.size(); i++){if( i == 0 && resultList.get(0) == 0){}else{System.out.print("" + resultList.get(i));}}}}public List<Integer> computeFactorial(int paramNumber){List<Integer> localResult = new ArrayList<Integer>();if(paramNumber == 4){localResult.add(4);localResult.add(0, 2);return localResult;}else{List<Integer> multiplier = convertNumber(paramNumber);List<Integer> multiplicand = computeFactorial(paramNumber - 1);for(int i = multiplier.size() - 1; i >= 0; i--){for(int j = multiplicand.size() - 1; j >= 0; j--){int temp = 0;int result = multiplier.get(i) * multiplicand.get(j);if(result > 10){temp = result%10;result = result/10;}else{if(result == 10){result = 1;temp = 0;}else{temp = result;result = 0;}}if(localResult.size() == 0){localResult.add(temp);if(result != 0){localResult.add(0, result);}}else{int multiplierLength = multiplier.size() - 1 - i ;int multiplicandLength = multiplicand.size() - 1 - j;int insertNumber;int numberPast;if(multiplierLength == 0 || multiplicandLength == 0){numberPast = Math.max(multiplierLength, multiplicandLength);}else{numberPast = (multiplierLength + multiplicandLength);}insertNumber = localResult.size() - numberPast - 1;if(insertNumber == 0){int localParam1 = localResult.get(insertNumber) + temp;if(localParam1 >= 10){result = result + 1;localParam1 = localParam1%10;}localResult.set(insertNumber, localParam1);if(result != 0){localResult.add(0, result);}}else if(insertNumber == -1){localResult.add(0, temp);localResult.add(0, result);}else if(insertNumber == -2){localResult.add(0, 0);localResult.add(0, 0);localResult.add(0, temp);localResult.add(0, result);} else{int localParam2 = localResult.get(insertNumber) + temp;if(localParam2 >= 10){result++;localParam2 = localParam2%10;}localResult.set(insertNumber, localParam2);if(insertNumber == 0){localResult.add(0, result);}else{localResult.set(insertNumber - 1, (localResult.get(insertNumber - 1) + result));}}for( int r = localResult.size() - 1; r >= 0; r--){if(r == 0 && localResult.get(0) == 0){localResult.remove(0);}if(localResult.get(r) >= 10){int temp1 = localResult.get(r)%10;int result1 = localResult.get(r)/10;if(r == 0){localResult.set(0, temp1);localResult.add(0, 1);}else{localResult.set(r, temp1);localResult.set(r - 1, localResult.get(r - 1) + result1);}}}}}}return localResult;}}private List<Integer> convertNumber(int paramNumber){int remainder = paramNumber%10;int dValue = (paramNumber - remainder)/10;List<Integer> convertedNumber = new ArrayList<Integer>();convertedNumber.add(remainder);paramNumber = dValue;while(dValue != 0){if(dValue == 10){remainder = 0;dValue = 1;}else{remainder = paramNumber%10;convertedNumber.add(0, remainder);dValue = (paramNumber - remainder)/10;paramNumber = dValue;}}return convertedNumber;}}