二进制整数加减乘除&除法算法证明&整除性证明
1、迭代法实现简单乘法
不用乘号只用加号实现乘法算法
代码
//迭代版本的简单乘法
//by LMR----20210914
#include<iostream>
#include<stdlib.h>
using namespace std;
//Function:odd_even
//input: The B from (int)A*(int)B
//output: B=odd:return 1;B=even:return 0
bool odd_even(int OorE)
{
if(OorE%2==1)
return true;
else
return false;
}
//Function:SimpleMultiply
//input: A(int) & B(int) from A*B
//output: Result=A*B
int SimpleMultiply(int A,int B)
{
int Result=0;
if(odd_even(B)==0)
{
for(int i=1;i<=B/2;i++)
Result+=A;
Result*=2;
}
else
{
for(int j=1;j<=B/2;j++)
Result+=A;
Result*=2;
Result+=A;
}
return Result;
}
//Function:CircleMult
//To run the multiply circulately untill the user stop it
//
void CircleMult(int &A,int &B)
{
//AgainOrNot:=1->again/=0->break
short int AgainOrNot=0;
cout<<"Caculate:A*B\n"
<<"A=";cin>>A;
cout<<"B=";cin>>B;
cout<<"A*B="<<SimpleMultiply(A,B)<<endl
<<"caculate again?(YorN:1/0)";cin>>AgainOrNot;
if(AgainOrNot==1)
{
system("cls");
CircleMult(A,B);
}
}
int main()
{
int A=0,B=0; //A*B
CircleMult(A,B);
return 0;
}
运行结果
2、证明除法算法
除法算法
对任意给定的整数a和b,其中b>0,存在唯一的整数对q(商)和r(余
数)使得,
a=q*b+r
且0≤r<b。
证明
证明0≤r<b
构造集合S={m=a-b*k|k∈Z} T={m∈S|m≥0},a>b是默认的,所以T非
空。作为一个元素都为正整数,且元素构成等差数列的集合,T中必然
存在最小元素,是为r=a-b*q,因为r∈T,所以r≥0。若r大于等于b,
则r不是T中最小元素,所以r<b。因而0≤r<b。
证明q、r唯一性
假设存在两对(q_1,r_1),(q_2,r_2)满足a=b*q+r
a=b*q_1+r_1 a=b*q_2+r_2
有 r_2-r_1=b*(q_1-q_2)
q_1!=q_2,则q_1-q_2!=0,所以b|(r_2-r_1)
又因为0≤r<b,所以-b<r_2-r_1<b
得r_2-r_1=0,如此才满足0≤r<b和b|(r_2-r_1)
所以q、r是唯一的。
3、证明整除性
命题整除性
①设a,b,c∈Z,如果 a|b,b|c,则a|c。
②如果c|a,c|b,则对任意m,n∈Z,有c|(ma+nb)
证明
①
有 b=a*p c=b*q (p,q∈Z)
因而c=a*p*q (p*q∈Z)
即a|c, ①得证
②
有 a=c*p b=c*q (p,q∈Z)
对于任意m,n∈Z
m*a=c*p*m,n*b=c*q*n
m*a+n*b=c*(p*m+q*n) (p*m+q*n∈Z)
即 c|{c*(p*m+q*n)}
即c|(m*a+n*b),②得证