. Net Big Data addition, subtraction, multiplication, division

Source: Internet
Author: User

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.

Related Article

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.