Multiplication between large numbers-Calculation of large numbers

Source: Internet
Author: User

A few days ago, my friend encountered a big number computing problem and I wrote it in a hurry.

To tell the truth, the Code is not very well written. It is a bit messy. Post it first, and then modify it in a few days.

 

Such as calculation: 9999999999999*1234567890123

  1. Package test;
  2. Public class bignumber {
  3. Public static double [] d1 = {9, 9, 9, 9, 9, 9, 9 };
  4. Public static double [] D2 = {1, 2, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3 };
  5. Public static int [] calculate (double [] D1, double [] D2 ){
  6. Int result_length = 0;
  7. Int result_num = 0;
  8. Double [] d1_temp = NULL;
  9. Double [] d2_temp = NULL;
  10. Int [] result_over = NULL;
  11. If (d1.length = d2.length ){
  12. Result_length = d1.length;
  13. Result_num = result_length;
  14. D1_temp = D1;
  15. D2_temp = d2;
  16. } Else {
  17. Result_length = (d1.length> = d2.length )? D1.length: d2.length;
  18. Result_num = (d1.length <= d2.length )? D1.length: d2.length;
  19. D1_temp = (d1.length <= d2.length )? D1: D2;
  20. D2_temp = (d1.length> = d2.length )? D1: D2;
  21. }
  22. Double [] [] result = new double [result_num] [result_length + 1];
  23. Int temp = 0;
  24. For (INT I = result_num-1; I> = 0; I --){
  25. For (Int J = result_length-1; j> = 0; j --){
  26. Temp + = (INT) (d1_temp [I] * d2_temp [J]);
  27. Result [I] [J + 1] = TEMP % 10;
  28. Temp = temp/10;
  29. }
  30. Result [I] [0] = temp;
  31. Temp = 0;
  32. }
  33. Temp = 0;
  34. Int flag = 0;
  35. Result_over = new int [result_num + result_length];
  36. Int line_flag = result_over.length-1;
  37. For (int m = Result [0]. Length-1; m> = 0; m --){
  38. Flag = m;
  39. For (INT n = result. Length-1; n> = 0; n --){
  40. If (flag <= (result [N]. Length-1 )){
  41. Temp + = Result [N] [flag];
  42. } Else {
  43. Break;
  44. }
  45. Flag ++;
  46. }
  47. Result_over [line_flag --] = TEMP % 10;
  48. Temp = temp/10;
  49. If (M = 0 ){
  50. If (result. length> = 2 ){
  51. For (int A = result. Length-2; a> = 0; --){
  52. Flag = 0;
  53. Temp + = Result [a] [flag ++];
  54. For (int B = A-1; B> = 0; B --){
  55. If (flag <= (result [B]. Length-1 )){
  56. Temp + = Result [B] [flag];
  57. } Else {
  58. Break;
  59. }
  60. Flag ++;
  61. }
  62. Result_over [line_flag --] = TEMP % 10;
  63. Temp = temp/10;
  64. }
  65. }
  66. }
  67. }
  68. Return result_over;
  69. }
  70. Public static void main (string [] ARGs ){
  71. Int [] d_result = calculate (D1, D2 );
  72. For (INT I = 0; I <d_result.length; I ++) {// output result
  73. System. Out. Print ("" + d_result [I] + "");
  74. }
  75. }
  76. }

 

-----------------------

Result: 1 2 3 4 5 6 7 8 9 0 1 2 8 7 6 5 4 3 2 1 0 9 8 7 7

 

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.