完成了大数乘法,过段时间完成一下阶乘和分数相关的函数
This commit is contained in:
@@ -18,8 +18,26 @@ struct fn {
|
|||||||
|
|
||||||
#define BN_ZERO (struct bn){NULL,0}
|
#define BN_ZERO (struct bn){NULL,0}
|
||||||
|
|
||||||
|
bool bn_bigger(struct bn _bn,unsigned int _i){
|
||||||
|
for(int i=1;i<_bn._i;i++){
|
||||||
|
if(_bn._p[i]!=0){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _bn._p[0]>_i;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bn_smaller(struct bn _bn,unsigned int _i){
|
||||||
|
for(int i=1;i<_bn._i;i++){
|
||||||
|
if(_bn._p[i]!=0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _bn._p[0]<_i;
|
||||||
|
}
|
||||||
|
|
||||||
struct bn bn_add_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int _nn){
|
struct bn bn_add_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int _nn){
|
||||||
if(output->_i==NULL){
|
if(output->_p==NULL){
|
||||||
output->_p=malloc(_bn._i*sizeof(unsigned int));
|
output->_p=malloc(_bn._i*sizeof(unsigned int));
|
||||||
output->_i=_bn._i;
|
output->_i=_bn._i;
|
||||||
}
|
}
|
||||||
@@ -56,7 +74,7 @@ struct bn bn_add_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigne
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset,unsigned int _p1){
|
struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset,unsigned int carry){
|
||||||
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i:_bn2._i;
|
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i:_bn2._i;
|
||||||
if(output->_p==NULL){
|
if(output->_p==NULL){
|
||||||
output->_p=malloc(_ni*sizeof(unsigned int));
|
output->_p=malloc(_ni*sizeof(unsigned int));
|
||||||
@@ -71,11 +89,11 @@ struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned lon
|
|||||||
}
|
}
|
||||||
unsigned long res;
|
unsigned long res;
|
||||||
if(offset<_bn1._i&&offset<_bn2._i){
|
if(offset<_bn1._i&&offset<_bn2._i){
|
||||||
res = (unsigned long)_bn1._p[offset]+(unsigned long)_bn1._p[offset]+_p1;
|
res = (unsigned long)_bn1._p[offset]+(unsigned long)_bn2._p[offset]+carry;
|
||||||
}else if(offset>=_bn1._i){
|
}else if(offset>=_bn1._i){
|
||||||
res = (unsigned long)_bn2._p[offset]+_p1;
|
res = (unsigned long)_bn2._p[offset]+carry;
|
||||||
}else{
|
}else{
|
||||||
res = (unsigned long)_bn1._p[offset]+_p1;
|
res = (unsigned long)_bn1._p[offset]+carry;
|
||||||
}
|
}
|
||||||
if(res>UINT32_MAX){
|
if(res>UINT32_MAX){
|
||||||
if(offset>=output->_i-1){
|
if(offset>=output->_i-1){
|
||||||
@@ -102,6 +120,107 @@ struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned lon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct bn bn_muti_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int carry,unsigned int _nn){
|
||||||
|
if(output->_p==NULL){
|
||||||
|
output->_p = malloc((_bn._i)*sizeof(unsigned int));
|
||||||
|
output->_i = _bn._i;
|
||||||
|
}
|
||||||
|
if(output->_i<_bn._i){
|
||||||
|
unsigned int* _np = realloc(output->_p,_bn._i*sizeof(unsigned int));
|
||||||
|
if(_np!=NULL){
|
||||||
|
output->_p = _np;
|
||||||
|
output->_i = _bn._i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(bn_bigger(*output,0)&&offset==0){
|
||||||
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
||||||
|
}
|
||||||
|
unsigned long res = ((unsigned long)_bn._p[offset])*_nn+carry;
|
||||||
|
if(offset<_bn._i-1){
|
||||||
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
||||||
|
bn_muti_nn(_bn,output,offset+1,res/((unsigned long)UINT32_MAX+1),_nn);
|
||||||
|
return *output;
|
||||||
|
}else{
|
||||||
|
if(res>UINT32_MAX){
|
||||||
|
unsigned int* _np = realloc(output->_p,(_bn._i+1)*sizeof(unsigned int));
|
||||||
|
if(_np!=NULL){
|
||||||
|
output->_p = _np;
|
||||||
|
output->_i = _bn._i+1;
|
||||||
|
}
|
||||||
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
||||||
|
output->_p[offset+1] = res/((unsigned long)UINT32_MAX+1);
|
||||||
|
return *output;
|
||||||
|
}else{
|
||||||
|
output->_p[offset] = res;
|
||||||
|
return *output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct bn bn_left_move(struct bn* _bn,unsigned long offset){
|
||||||
|
if(offset==0){
|
||||||
|
return *_bn;
|
||||||
|
}
|
||||||
|
unsigned int* _np = realloc(_bn->_p,(_bn->_i+offset)*sizeof(unsigned int));
|
||||||
|
if(_np!=NULL){
|
||||||
|
_bn->_p = _np;
|
||||||
|
_bn->_i += offset;
|
||||||
|
}
|
||||||
|
for(int i=_bn->_i-offset-1;i>=0;i--){
|
||||||
|
_bn->_p[i+offset] = _bn->_p[i];
|
||||||
|
}
|
||||||
|
memset(_bn->_p,0,offset*sizeof(unsigned int));
|
||||||
|
return *_bn;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct bn bn_muti_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset){
|
||||||
|
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i*2:_bn2._i*2;
|
||||||
|
if(output->_p==NULL){
|
||||||
|
output->_p = malloc(_ni*sizeof(unsigned int));
|
||||||
|
output->_i = _ni;
|
||||||
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
||||||
|
}
|
||||||
|
if(output->_i < _ni){
|
||||||
|
unsigned long old_i = output->_i;
|
||||||
|
unsigned int* _np = realloc(output->_p, _ni * sizeof(unsigned int));
|
||||||
|
if(_np != NULL){
|
||||||
|
if(_ni > old_i){
|
||||||
|
memset(_np + old_i, 0, (_ni - old_i) * sizeof(unsigned int));
|
||||||
|
}
|
||||||
|
output->_p = _np;
|
||||||
|
output->_i = _ni;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(bn_bigger(*output,0)&&offset==0){
|
||||||
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
||||||
|
}
|
||||||
|
struct bn res = BN_ZERO,carry = BN_ZERO;
|
||||||
|
if(offset<_bn2._i-1){
|
||||||
|
bn_muti_nn(_bn1,&res,0,0,_bn2._p[offset]);
|
||||||
|
bn_left_move(&res,offset);
|
||||||
|
bn_add_bn(*output,res,&carry,0,0);
|
||||||
|
unsigned int* _np = realloc(output->_p,carry._i*sizeof(unsigned int));
|
||||||
|
if(_np!=NULL){
|
||||||
|
output->_p = _np;
|
||||||
|
output->_i = carry._i;
|
||||||
|
}
|
||||||
|
memcpy(output->_p,carry._p,output->_i*sizeof(unsigned int));
|
||||||
|
bn_muti_bn(_bn1,_bn2,output,offset+1);
|
||||||
|
return *output;
|
||||||
|
}else{
|
||||||
|
bn_muti_nn(_bn1,&res,0,0,_bn2._p[offset]);
|
||||||
|
bn_left_move(&res,offset);
|
||||||
|
bn_add_bn(*output,res,&carry,0,0);
|
||||||
|
unsigned int* _np = realloc(output->_p,carry._i*sizeof(unsigned int));
|
||||||
|
if(_np!=NULL){
|
||||||
|
output->_p = _np;
|
||||||
|
output->_i = carry._i;
|
||||||
|
}
|
||||||
|
memcpy(output->_p,carry._p,output->_i*sizeof(unsigned int));
|
||||||
|
return *output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct bn nn2bn(unsigned long _i){
|
struct bn nn2bn(unsigned long _i){
|
||||||
if(_i>UINT32_MAX){
|
if(_i>UINT32_MAX){
|
||||||
unsigned int* _p=malloc(2*sizeof(int));
|
unsigned int* _p=malloc(2*sizeof(int));
|
||||||
@@ -149,24 +268,6 @@ struct bn bn_division(struct bn _bn,struct bn last_result,unsigned long offset,u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bn_bigger(struct bn _bn,unsigned int _i){
|
|
||||||
for(int i=1;i<_bn._i;i++){
|
|
||||||
if(_bn._p[i]!=0){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return _bn._p[0]>_i;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool bn_smaller(struct bn _bn,unsigned int _i){
|
|
||||||
for(int i=1;i<_bn._i;i++){
|
|
||||||
if(_bn._p[i]!=0){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return _bn._p[0]<_i;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* bn2string(struct bn _i){//don't forget free this!!!
|
char* bn2string(struct bn _i){//don't forget free this!!!
|
||||||
char* res = malloc(_i._i*10);
|
char* res = malloc(_i._i*10);
|
||||||
struct bn n1,n2;
|
struct bn n1,n2;
|
||||||
@@ -206,8 +307,9 @@ struct fn Chudnovsky(unsigned long _k){
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
struct bn n1 = nn2bn(0xFFFFFFFFFFFFFFFFULL);
|
struct bn n1 = nn2bn(0xFFFFFFFFFFFFFFFFULL);
|
||||||
struct bn n2 = bn_add_nn(n1,&BN_ZERO,0,0xFFFFFFFFU);
|
struct bn n2 = bn_muti_nn(n1,&BN_ZERO,0,0,0xffffffff);
|
||||||
char* out = bn2string(n2);
|
struct bn n3 = bn_muti_bn(n1,n2,&n3,0);
|
||||||
printf("%s",out);
|
char* out = bn2string(n3);
|
||||||
|
printf("%s\n",out);
|
||||||
free(out);
|
free(out);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user