(Zz) using the division and Control Method for the multiplication of large numbers (Java)

Source: Internet
Author: User
/*************************************** **********************
* This program uses the divide and conquer method for multiplication.
* 1011*1101
* Coder: jiutian X-Dragon
* Running result:
* H:/Java/algorithm/big integer ~ 1> javac mult. Java
*
* H:/Java/algorithm/big integer ~ 1> JAVA Mult
* 11x13: 143
*
* H:/Java/algorithm/big integer ~ 1>
**************************************** **********************/
Import java. Lang .*;
Class mult {

Public int X, Y; // The two numbers that require product.

// Initialize the two numbers required in this question
Mult (){
X = 11; // 1011 of binary data
Y = 13; // 1101 of Binary
}

//************************************** ************
// Function name getvalue. This function is the main function of this algorithm.
// The reverse return value is the product of the specified number XY.
// According to the formula xy = A * C * 2n + (A-B) (D-C) + AC + bd) * 2n/2 + BD
// There are 3 multiplication, 6 addition, and 2 shift operations in total
//************************************** ************

Public int getvalue (int A, int B, int C, int d ){
Int N = 4; // The number of digits of the desired number

Return (A * C * (2 <(n-1) +
(A-B) * (D-C) + A * C + B * D )*
(2 <(n/2-1) +
B * D );
}

Public static void main (string [] ARGs ){
Int n = 2;
Mult mu = new mult ();
Int A, B, C, D;
// Break down the original Multiplier
A = mu. x> N;
B = mu. X & 3;
C = mu. Y> N;
D = mu. Y & 3;

Int ret = mu. getvalue (A, B, C, D );

// Output result
System. Out. println ("" + Mu. x + "X" + Mu. Y + "Result:" + RET );
}
}

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.