The idea of division and control is generally used in algorithm textbooks, and the multiplication of large numbers is often used as a good example of the idea of division and control.
The details are as follows:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/161UB0Y-0.png "height =" 432 "/>
Although the above principle corresponds to a binary system, it is also feasible for a 10-digit system.
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" 745f289e%648a38856c963432419f4 "border =" 0 "alt =" 745f289e%648a38856c963432419f4 "src =" http://www.bkjia.com/uploads/allimg/131228/161U61W7-1.jpg "height =" 311 "/>
Use C # To make full use of the features of C. In this example, as long as the number to be split is smaller than 9 digits, the number can be multiplied directly to avoid overflow.
In programming, addition and subtraction are also required, which must be achieved through string simulation.
The final Multiplication operation relies on recursion.
The code in this article also has some optimizations. For example, it may be faster to use arrays instead of strings.
The Code is as follows:
Namespace bigIntMultiply {class Program {static void Main (string [] args) {string a = "99999999999999"; string B = "123456789001234567890"; Stopwatch sw = new Stopwatch (); sw. start (); string s = Multiply (B, B); sw. stop (); Console. writeLine (s); Console. writeLine (sw. elapsed);} // string multiplication simulation static string Multiply (string x, string y) {// deep ++; // Console. writeLine ("-" + deep + "-"); string negative = ""; If (x. StartsWith ("-") & y. StartsWith ("-") | (! X. StartsWith ("-")&&! Y. startsWith ("-") {x = x. trimStart ('-'); y = y. trimStart ('-'); negative = "";} else if (x. startsWith ("-")&&! Y. StartsWith ("-") | (! X. startsWith ("-") & y. startsWith ("-") {x = x. trimStart ('-'); y = y. trimStart ('-'); negative = "-";} // If the length is smaller than 9, multiply them directly and return the result. If (x. length <= 9 & y. length <= 9) {long tmp = (long. parse (x) * long. parse (y); if (tmp = 0) return tmp. toString (); return negative + (long. parse (x) * long. parse (y )). toString () ;}// the abcd string a, B, c, d in the formula; if (x. length <= 9) {a = "0"; B = x;} else {if (x. length % 2! = 0) x = "0" + x; a = x. substring (0, x. length/2); B = x. substring (x. length/2);} if (y. length <= 9) {c = "0"; d = y;} else {if (y. length % 2! = 0) y = "0" + y; c = y. substring (0, y. length/2); d = y. substring (y. length/2);} int n = x. length> = y. length? X. Length: y. Length; string t1, t2, t3; // recursive call. The value is calculated based on the formula. String ac = Multiply (a, c); string bd = Multiply (B, d); t1 = Multiply (Subtract (a, B), Subtract (d, c )); t2 = Add (t1, ac), bd); t3 = Add (Power10 (ac, n), Power10 (t2, n/2), bd ). trimStart ('0'); if (t3 = "") return "0"; return negative + t3;} // simulate the addition operation of the string static string Add (string x, string y) {if (x. startsWith ("-")&&! Y. StartsWith ("-") {return Subtract (y, x. TrimStart ('-');} else if (! X. startsWith ("-") & y. startsWith ("-") {return Subtract (x, y. trimStart ('-');} else if (x. startsWith ("-") & y. startsWith ("-") {return "-" + Add (x. trimStart ('-'), y. trimStart ('-');} if (x. length> y. length) {y = y. padLeft (x. length, '0');} else {x = x. padLeft (y. length, '0');} int [] sum = new int [x. length + 1]; for (int I = x. length-1; I> = 0; I --) {int tmpsum = int. parse (x [I]. toString () + Int. parse (y [I]. toString () + sum [I + 1]; if (tmpsum> = 10) {sum [I + 1] = tmpsum-10; sum [I] = 1; // indicates carry} else {sum [I + 1] = tmpsum ;}} string returnvalue = string. concat (sum); if (sum [0] = 1) {return returnvalue;} else {return returnvalue. remove (0, 1) ;}/// string simulated subtraction operation static string Subtract (string x, string y) {// if (x. startsWith ("-")&&! Y. startsWith ("-") // {// return "-" + Add (x. trimStart ('-'), y); //} // if (y. startsWith ("-") // {// return Add (x, y. trimStart ('-'); // x is a positive number, and y is also a positive number int flag = checkBigger (x, y); if (flag = 0) {return "0";} else if (flag =-1) {string tmp = y; y = x; x = tmp ;} // ensure that x> = y. padLeft (x. length, '0'); // y zeros and x alignment int [] difference = new int [x. length]; for (int I = x. length-1; I> = 0; I --) {int tmpdifference; tmpdifference = int. parse (x [I]. toString ()-int. parse (y [I]. toString () + difference [I]; if (tmpdifference <0) {tmpdifference + = 10; difference [I-1] =-1; // indicates the borrow space} difference [I] = tmpdifference;} StringBuilder returnvalue = new StringBuilder (string. concat (difference ). trimStart ('0'); {if (returnvalue. toString () = "") {return "0" ;}} if (flag =-1) {returnvalue = returnvalue. insert (0, "-");} return returnvalue. toString () ;}// compare the size of static int checkBigger (string x, string y) {if (x. length> y. length) {return 1;} else if (x. length <y. length) {return-1;} else {for (int I = 0; I <x. length; I ++) {if (int. parse (x [I]. toString ()> int. parse (y [I]. toString () {return 1;} else if (int. parse (x [I]. toString () <int. parse (y [I]. toString () {return-1;} continue;} return 0 ;}// simulate the shift of static string Power10 (string num, int n) {return num. padRight (num. length + n, '0 ');}}}
Tested 1234567890 ......... the square of 1234567890 (260 1234567890,2600 digits) is calculated as follows: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/161UC1E-2.png "title =" capture. PNG "/>
You can obtain the verification code from https://defuse.ca/big-number-calculator.htm.
This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1201901