In. Net 4.0, there is a method that encapsulates large numbers of operations, and the efficiency is super fast.
BigInteger a = BigInteger.Parse("124548787123123123335723122");
BigInteger b = BigInteger.Parse("7852132487452222222222222221440231333");
txtmsg.Text += "b/a=" + BigInteger.Divide(b, a).ToString() + "\r\n";
txtmsg.Text += "b+a=" + BigInteger.Add(b, a).ToString() + "\r\n";
txtmsg.Text += "b-a=" + BigInteger.Subtract(b, a).ToString() + "\r\n";
txtmsg.Text += "b*a=" + BigInteger.Multiply(b, a).ToString() + "\r\n";
txtmsg.Text += "b%a=" + BigInteger.Remainder(b, a).ToString() + "\r\n";
Add reference:
using System.Numerics;
An experiment was conducted using the multiplication of large numbers:
Calculate the product of all prime numbers within 1000:
Implement the multiplication step by yourself. Multiply each bit of a large number by the second one and add the result. The result is 100 times slower than the official speed.
Int n = 1000;
Int [] nums = GetAllKNum (n );
// Your own soil Method
Methodone one = new methodone ();
Stopwatch wc1 = new Stopwatch ();
Wc1.Start ();
String result1 = one. P (n, nums );
Wc1.Stop ();
Long long1 = wc1.ElapsedMilliseconds;
//. Net4.0 Method
Methodtwo two = new methodtwo ();
Wc1.Reset ();
Wc1.Start ();
String result2 = two. P (n, nums );
Wc1.Stop ();
Long long2 = wc1.ElapsedMilliseconds;
// Display the result
Txtmsg. Text = "";
Txtmsg. Text + = "your soil method: \ r \ n ";
Txtmsg. Text + = "Result:" + result1 + "\ r \ n ";
Txtmsg. Text + = "Time consumed:" + long1.ToString () + "millisecond \ r \ n ";
Txtmsg. Text + = ". net4.0 method: \ r \ n ";
Txtmsg. Text + = "Result:" + result2 + "\ r \ n ";
Txtmsg. Text + = "Time consumed:" + long2.ToString () + "millisecond \ r \ n ";
Result:
I am ashamed to say that my method is more than 100 times slower than the official method, and I don't know how the official method is implemented.