当前位置:网站首页>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—————
边栏推荐
猜你喜欢
Complete solution instance of Oracle shrink table space
机器学习01:绪论
Rider 设置选中单词侧边高亮,去除警告建议高亮
Embedded-c Language-2
Error in composer installation: no composer lock file present.
查看自己电脑连接过的WiFi密码
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
Tips for extracting JSON fields from MySQL
Machine learning compilation lesson 2: tensor program abstraction
Learn about MySQL transaction isolation level
随机推荐
Read the basic grammar of C language in one article
Little knowledge about C language (array and string)
How to write a full score project document | acquisition technology
Rider 设置选中单词侧边高亮,去除警告建议高亮
Complete solution instance of Oracle shrink table space
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
Embedded -arm (bare board development) -1
Flask solves the problem of CORS err
漫画:寻找无序数组的第k大元素(修订版)
First day of learning C language
域名解析,反向域名解析nbtstat
NPM installation
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
一文了解MySQL事务隔离级别
Check the WiFi password connected to your computer
[binary tree] insufficient nodes on the root to leaf path
The second day of learning C language for Asian people
Embedded-c language-6
Embedded -arm (bare board development) -2
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible