java 大數階乘(二)

來源:互聯網
上載者:User

由於大數的階乘的結果比較大,長整型是不能儲存的下的,為了能實現這個功能,使用按位進行相乘,將沒位的結果分開存放,最後輸出,不過運算的速度有點慢,算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;}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.