当前位置:网站首页>[original] BigInteger. Large number multiplication. Large number operation. "Infinite number" multiplication. Comparison of two methods of large number multiplication
[original] BigInteger. Large number multiplication. Large number operation. "Infinite number" multiplication. Comparison of two methods of large number multiplication
2022-04-21 20:03:00 【delacrxoix_ xu】
I've been looking at some test questions recently , I know that the operation of large numbers is a frequently tested subject . So I was interested in trying .
At first, I wrote a by myself according to the written calculation method , But the time complexity is o(n3).
After referring to the online algorithm , Modified its own algorithm , Time complexity becomes o(n2).
In the following test results , Two 2000 Digit number ( The number of digits in Arabic numerals ) Multiply , Time consuming 90 Many milliseconds .
200 position ,1 millisecond . You can see , Complexity is indeed N The square level of .
Write your own stupid way , After each accumulation, it is necessary to judge whether there is carry . But it's safe .
There is an efficient algorithm on the Internet , Use int Store temporary results , You don't have to judge a carry after each accumulation . etc.
Judge after all the accumulation is completed , So the time complexity is reduced by an order of magnitude .
But there is also a disadvantage , Namely int Sooner or later, there will be overflow , When the number of digits of an integer is enough ,
That is to say, we have achieved 2 Of 29、30、31、32 Secondary orientation ( Of course, this is almost impossible ), The result of this method is wrong .
The following is the function corresponding to the algorithm .
char* BigIntMultiply ( const char * const a, const char * const b , char* const lresult)
{
int i,j;
int la = strlen(a);
int lb = strlen(b);
int rlen = la+lb;
int* r = (int*)calloc( rlen, sizeof(int) );
for(i = 0;i < lb; i++)
for(j = 0; j < la; j++)
r[rlen - 1 - i - j] += (b[lb - i - 1] - '0') * (a[la - j - 1] - '0');
//then is there carry on current number
for(j = rlen - 1; j >= 1; j--)
if(r[j] > 9)
{
r[j-1] += r[j] / 10;
r[j] %= 10;
}
//find first none_zero_index
for(i = 0; 0 == r[i]; i++){}
//mem cpy
for(j=0; i< rlen; i++,j++)
lresult[j] = r[i]+'0';
lresult[j]='\0';
free(r);
return lresult;
}
The following code is in Visual Studio 2008 Compile and run inside , No problem . Linux Not on SYSTEMTIME,
No, atoi,itoa,GetLocalTime. So we need to Linux function , It has to be revised accordingly .
#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <malloc.h>
using namespace std;
const int MAX = 2001;
char num1[MAX];
char num2[MAX];
char result[2*MAX];
void SafeGetNumFromStr ( char* num, char* str);
char* BigIntMultiply ( const char * const a, const char * const b , char* const lresult);
void multiply( const char *a, const char *b);
int main(int argc, char *argv[])
{
//test speed
cout<<"\n\nspeed test... Number of digits is : "<<MAX-1<<"\n";
int i;
const int TEST_TIME = 20;
srand((unsigned)time(NULL));
for(i = 0;i<MAX;i++)
{
num1[i] = 0;
num2[i] = 0;
}
//create data with random
for(i = 0; i< MAX - 1; i++)
{
num1[i] = rand()%10 + '0';
num2[i] = rand()%10 + '0';
}
SYSTEMTIME wtm;
GetLocalTime(&wtm);
long long time_start = wtm.wMilliseconds + wtm.wSecond * 1000;
cout<<num1<<endl;
cout<<"*\n";
cout<<num2<<endl;
for(i = 0; i<TEST_TIME; i++)
{
BigIntMultiply(num1,num2,result);
}
GetLocalTime(&wtm);
cout<<"Result is:\n";
cout<<result<<endl;
double tmv = (double)(wtm.wMilliseconds + wtm.wSecond * 1000 - time_start);
cout<<"Test Over. "<<TEST_TIME<<" loops use time: "<<tmv<<" ms\n";
cout<<" Each One Time Use: "<<tmv/(double)TEST_TIME<<" ms\n\n\n";
//test validation
cout<<"Validation work...\n";
long long testNum1;
long long testNum2;
int testI;
for(testNum1 = 0;testNum1<1000000000000000;testNum1 = (testNum1+1)*181+1)
for(testI= 0;testI<200; testI++)
{
char a[2*MAX];
char b[2*MAX];
testNum2 = (testNum1+testI)<0?0:testNum1+testI;
for(i = 0;i<MAX;i++)
{
num1[i] = 0;
num2[i] = 0;
}
itoa(testNum1,a,10);
itoa(testNum2,b,10);
SafeGetNumFromStr(num1,a);
SafeGetNumFromStr(num2,b);
BigIntMultiply(num1,num2,result);
if(8 == testNum2%10)
if(testNum1*testNum2 == atoi(result))
cout<<testNum1<<" * "<<testNum2<<" == "<<testNum1*testNum2<<" Correct!\n";
else
cout<<testNum1<<" * "<<testNum2<<" Result:"<<result<<"\n";
}
//free test
cout<<"Now ..... Free Test!\n";
while(1)
{
char a[2*MAX];
char b[2*MAX];
cout<<"\n\ninput long integer for A"<<endl;
cin>>a;
cout<<"input long integer for B"<<endl;
cin>>b;
//get data
SafeGetNumFromStr(num1,a);
SafeGetNumFromStr(num2,b);
cout<<endl<<endl;
cout<<num1;
cout<<" * ";
cout<<num2;
cout<<endl;
BigIntMultiply(num1,num2,result);
cout<<"Result is:"<<endl;
cout<<result;
}
system("pause");
return 0;
}
void SafeGetNumFromStr( char* num, char* str)
{
memset(num,0,sizeof(num[0])*MAX);
int i;
int index = 0;
for(i=0;i<2*MAX && index < MAX;i++)
{
if(str[i] <= '9' && str[i] >='0')
num[index++] = str[i];
if('\0'==str[i])
break;
}
assert( 0 != index );
}
char* BigIntMultiply ( const char * const a, const char * const b , char* const lresult)
{
int i,j;
int la = strlen(a);
int lb = strlen(b);
int rlen = la+lb;
int* r = (int*)calloc( rlen, sizeof(int) );
for(i = 0;i < lb; i++)
for(j = 0; j < la; j++)
r[rlen - 1 - i - j] += (b[lb - i - 1] - '0') * (a[la - j - 1] - '0');
//then is there carry on current number
for(j = rlen - 1; j >= 1; j--)
if(r[j] > 9)
{
r[j-1] += r[j] / 10;
r[j] %= 10;
}
//find first none_zero_index
for(i = 0; 0 == r[i]; i++){}
//mem cpy
for(j=0; i< rlen; i++,j++)
lresult[j] = r[i]+'0';
lresult[j]='\0';
free(r);
return lresult;
}



版权声明
本文为[delacrxoix_ xu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212001343455.html
边栏推荐
- 危化品企业双预防机制数字化建设综合解决方案
- How to judge whether the nbit bit of int type value is 1 or 0
- 界面组件Telerik UI for WPF入门指南 - 颜色主题生成器
- Dolphin DB vscode plug-in tutorial
- Fiddler's solution to the failure of capturing PC wechat applet
- 【时序】LSTNet:结合 CNN、RNN 以及 AR 的时间序列预测模型
- [play with lighthouse] use Tencent cloud to realize wechat payment business in light weight
- PyCharm failed to create JVM
- MYSQL输入密码后闪退的解决方法
- 如何在不加锁的情况下解决线程安全问题
猜你喜欢

高端制造业企业信息化解决方案,工业电商平台设备、数据、体系预测性维护

CUDA02 - 访存优化和Unified Memory

Meaning of stripe in image

界面组件Telerik UI for WPF入门指南 - 颜色主题生成器

MySQL 2003 can't connect to MySQL server on 'localhost' (10038)

nodejs笔记1

表面点云法线

Create thread pool manually

int count= cmd. ExecuteNonQuery(); There is a syntax error nearby

Mysql错误2005
随机推荐
[play with lighthouse] use Tencent cloud to realize wechat payment business in light weight
05.原型模式
Create thread pool manually
R language data analysis from entry to advanced: (8) data format conversion of data cleaning skills (including the conversion between wide data and long data)
webrtc入门:3.使用enumerateDevices获取设备中可用的流媒体
开源许可证热门及冷门问题接地气探讨
Jerry uses hardware timer to simulate interrupt request [chapter]
nodejs笔记1
Unity Socket
MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
PostgreSql postgres_fdw
艾尔登法环“无法加载保存数据”解决方法
Publicity of the winners of the 9th "Datang Cup" National College Students' mobile communication 5g technology competition
STL容器(一)--vector
快速排序的三种实现方式
Building / importing targets using cmake
牛客BM40. 重建二叉树
Gbase 8A round or reject the double value, and the result is not the analysis and solution of rounding problem
Interface non idempotent solution
2022下半年PMP考试开始报名,须知