当前位置:网站首页>Cartoon: how to multiply large integers? (I) revised version
Cartoon: how to multiply large integers? (I) revised version
2022-07-05 17:28:00 【Small ash】
In the new cartoon released by Xiaohui on Monday , There are two small problems :
1. about master The third condition of the theorem is incorrectly stated , Should be a+ε, instead of a-ε.
2. Divide and conquer T(n) and T(n/2) The relationship between master The first condition of the theorem , Calculation ε The process of value is wrong .
These two questions have been revised this time , Thank you very much for your correction .
Some time ago , A cartoon about the small sum of grey numbers was published , If you haven't seen it, you can have a look first :
comic : How to add big integers ?( Revised edition )
that , How to realize the multiplication of large integers ?
At first , Xiaohui thinks that as long as the idea of adding large integers is done a little deformation , You can easily multiply large integers . But with further study , Xiaohui found out that things are not so simple ......
————— the second day —————
How to list the multiplication column ? With 93281 X 2034 For example , The vertical form is as follows :
In the program , We can use int Type of the array , Store two large integers by bit , Then the elements in the array are calculated one by one like the primary vertical .
The calculation of the multiplication column can be roughly divided into two steps :
1. Integers B Every digit and integer of A All the digits are multiplied in turn , Get an intermediate result .
2. All the intermediate results add up , Get the final result .
/**
* The product of large integers
* @param bigNumberA Large integer A
* @param bigNumberB Large integer B
*/
public static String multiply(String bigNumberA, String bigNumberB) {
//1. Store two large integers in reverse order , The length of the array is equal to the sum of the lengths of two integers
int lengthA = bigNumberA.length();
int lengthB = bigNumberB.length();
int[] arrayA = new int[lengthA];
for(int i=0; i< lengthA; i++){
arrayA[i] = bigNumberA.charAt(lengthA-1-i) - '0';
}
int[] arrayB = new int[lengthB];
for(int i=0; i< lengthB; i++){
arrayB[i] = bigNumberB.charAt(lengthB-1-i) - '0';
}
//2. structure result Array , The length of the array is equal to the sum of the lengths of two integers
int[] result = new int[lengthA+lengthB];
//3. Nested loop , Integers B Each bit of, in turn, is an integer A Multiply all the digits of , And add up the results
for(int i=0;i<lengthB;i++) {
for(int j=0;j<lengthA;j++) {
// Integers B A bit and an integer of A Multiply one of the two
result[i+j] += arrayB[i]*arrayA[j];
// If result One is greater than 10, Then carry , The number of carry is the number of bits divided by 10 The business of
if(result[i+j] >= 10){
result[i+j+1] += result[i+j]/10;
result[i+j] = result[i+j]%10;
}
}
}
//4. hold result The array is reversed again and converted to String
StringBuilder sb = new StringBuilder();
// Whether to find the most significant bit of a large integer
boolean findFirst = false;
for (int i = result.length - 1; i >= 0; i--) {
if(!findFirst){
if(result[i] == 0){
continue;
}
findFirst = true;
}
sb.append(result[i]);
}
return sb.toString();
}
public static void main(String[] args) {
String x = "3338429042340042304302404";
String y = "12303231";
System.out.println(multiply(x, y));
}————————————
below , There will be some brain burning in our derivation , Please sit down and hold on ~~
Large integers go from high to low , It's divided into two parts . Set integer 1 The high part of is A, The lower part is B; Integers 2 The high part of is C, The lower part is D, So there's the following equation :
If we abstract the length of a large integer as n, that :
therefore , Integers 1 And integers 2 The product of can be written in the following form :
In this way , The original length was n Of large integers 1 Times product , Transformed into length n/2 Of large integers 4 Times product (AC,AD,BC,BD).
What is? master And the theorem ?
master The English name of the theorem is master theorem, It's for many The recurrence relation obtained by divide and conquer method Provides a progressive time complexity analysis .
Let's set a constant a >= 1,b > 1, If the overall computational scale of an algorithm T(n) = a T(n / b) + f(n), Then there are the following rules :
Suppose two lengths are n Multiply the large integers of , The overall operation scale is T(n) .
According to the conclusion just reached , The multiplication of two large integers is split into four smaller products :
So in the first partition ,T(n) and T(n/2) Has the following relation :
T(n) = 4T(n/2) + f(n)
among f(n) yes 4 The operation scale of adding the product results ,f(n) The incremental time complexity of is obviously O(n).
Bring this relationship to master The formula of the theorem T(n) = a T(n / b) + f(n) among ,
here a=4, b=2.
here , hold a and b Value , as well as f(n) Time complexity brought to master The first law of the theorem , That's the following rule :
It's just the right thing to do .
How does it fit ? The derivation process is as follows :
So our average time complexity is :
—————END—————
边栏推荐
- URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
- Example tutorial of SQL deduplication
- 【二叉树】根到叶路径上的不足节点
- CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
- About JSON parsing function JSON in MySQL_ EXTRACT
- Redis+caffeine two-level cache enables smooth access speed
- VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
- Error in compiling libssh2. OpenSSL cannot be found
- Embedded UC (UNIX System Advanced Programming) -2
- stirring! 2022 open atom global open source summit registration is hot!
猜你喜欢

Design of electronic clock based on 51 single chip microcomputer
Tips for extracting JSON fields from MySQL

Using C language to realize palindrome number

stirring! 2022 open atom global open source summit registration is hot!

7. Scala class
基于Redis实现延时队列的优化方案小结

CMake教程Step2(添加库)

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

Wsl2.0 installation

The second day of learning C language for Asian people
随机推荐
thinkphp3.2.3
Three traversal methods of binary tree
漫画:如何实现大整数相乘?(整合版)
项目引入jar从私服Nexus 拉去遇到的一个问题
High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
CMake教程Step4(安装和测试)
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
Embedded-c Language-5
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Tips for extracting JSON fields from MySQL
Machine learning compilation lesson 2: tensor program abstraction
Oracle缩表空间的完整解决实例
Example tutorial of SQL deduplication
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Mysql5.6 parsing JSON strings (supporting complex nested formats)
winedt常用快捷键 修改快捷键latex编译按钮
Force deduction solution summary 729- my schedule I
【二叉树】根到叶路径上的不足节点
ternary operator