213 lines
6.0 KiB
C
213 lines
6.0 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
struct bn {
|
|
unsigned int* _p;
|
|
unsigned long _i;
|
|
};
|
|
|
|
struct fn {
|
|
struct bn numerator;
|
|
struct bn denominator;
|
|
};
|
|
|
|
#define BN_ZERO (struct bn){NULL,0}
|
|
|
|
struct bn bn_add_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int _nn){
|
|
if(output->_i==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;
|
|
}
|
|
}
|
|
unsigned long res = (unsigned long)_bn._p[offset]+_nn;
|
|
if(res>UINT32_MAX){
|
|
if(offset>=_bn._i-1){
|
|
unsigned int* _np = realloc(output->_p,(output->_i+1)*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i +=1;
|
|
}
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
output->_p[offset+1] = 1;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
bn_add_nn(_bn,output,offset+1,1);
|
|
}
|
|
}else{
|
|
if(offset==_bn._i-1){
|
|
output->_p[offset] = res;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res;
|
|
bn_add_nn(_bn,output,offset+1,0);
|
|
}
|
|
}
|
|
}
|
|
|
|
struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset,unsigned int _p1){
|
|
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i:_bn2._i;
|
|
if(output->_p==NULL){
|
|
output->_p=malloc(_ni*sizeof(unsigned int));
|
|
output->_i=_ni;
|
|
}
|
|
if(output->_i<_ni){
|
|
unsigned int* _np = realloc(output->_p,_ni*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = _ni;
|
|
}
|
|
}
|
|
unsigned long res;
|
|
if(offset<_bn1._i&&offset<_bn2._i){
|
|
res = (unsigned long)_bn1._p[offset]+(unsigned long)_bn1._p[offset]+_p1;
|
|
}else if(offset>=_bn1._i){
|
|
res = (unsigned long)_bn2._p[offset]+_p1;
|
|
}else{
|
|
res = (unsigned long)_bn1._p[offset]+_p1;
|
|
}
|
|
if(res>UINT32_MAX){
|
|
if(offset>=output->_i-1){
|
|
unsigned int* _np = realloc(output->_p,output->_i+1);
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i +=1;
|
|
}
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
output->_p[offset+1] = 1;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
bn_add_bn(_bn1,_bn2,output,offset+1,1);
|
|
}
|
|
}else{
|
|
if(offset==output->_i-1){
|
|
output->_p[offset] = res;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res;
|
|
bn_add_bn(_bn1,_bn2,output,offset+1,0);
|
|
}
|
|
}
|
|
}
|
|
|
|
struct bn nn2bn(unsigned long _i){
|
|
if(_i>UINT32_MAX){
|
|
unsigned int* _p=malloc(2*sizeof(int));
|
|
_p[1]=_i>>32;
|
|
_p[0]=_i%((unsigned long)UINT32_MAX+1);
|
|
return (struct bn){_p,2};
|
|
}else{
|
|
unsigned int* _p=malloc(sizeof(int));
|
|
_p[0]=_i%((unsigned long)UINT32_MAX+1);
|
|
return (struct bn){_p,1};
|
|
}
|
|
}
|
|
|
|
unsigned int bn_remainder(struct bn _bn,unsigned long offset,unsigned int last_remainder,unsigned int divisor){
|
|
unsigned long res;
|
|
if(offset!=0){
|
|
res = _bn._p[_bn._i-1-offset]+((unsigned long)last_remainder<<32);
|
|
}else{
|
|
res = _bn._p[_bn._i-1-offset];
|
|
}
|
|
if(offset+1==_bn._i){
|
|
return res%divisor;
|
|
}else{
|
|
return bn_remainder(_bn,offset+1,res%divisor,divisor);
|
|
}
|
|
}
|
|
|
|
struct bn bn_division(struct bn _bn,struct bn last_result,unsigned long offset,unsigned int last_remainder,unsigned int divisor){
|
|
unsigned long res;
|
|
if(offset!=0){
|
|
res = _bn._p[_bn._i-1-offset]+((unsigned long)last_remainder<<32);
|
|
}else{
|
|
res = _bn._p[_bn._i-1-offset];
|
|
}
|
|
if(last_result._p==NULL){
|
|
last_result._p=malloc(_bn._i*sizeof(unsigned int));
|
|
memset(last_result._p,0,_bn._i*sizeof(unsigned int));
|
|
last_result._i=_bn._i;
|
|
}
|
|
last_result._p[last_result._i-offset-1] = res/divisor;
|
|
if(offset+1==_bn._i){
|
|
return last_result;
|
|
}else{
|
|
return bn_division(_bn,last_result,offset+1,res%divisor,divisor);
|
|
}
|
|
}
|
|
|
|
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* res = malloc(_i._i*10);
|
|
struct bn n1,n2;
|
|
n1._p = malloc(_i._i*sizeof(unsigned int));n1._i=_i._i;n2._p = malloc(_i._i*sizeof(unsigned int));n2._i=_i._i;
|
|
memcpy(n1._p,_i._p,n1._i*sizeof(unsigned int));
|
|
memset(n2._p,0,n2._i*sizeof(unsigned int));
|
|
unsigned long char_offset = 0;
|
|
while(true){
|
|
res[char_offset] = bn_remainder(n1,0,0,10);
|
|
if(bn_smaller(n1,10)){
|
|
break;
|
|
}
|
|
char_offset++;
|
|
bn_division(n1,n2,0,0,10);
|
|
memset(n1._p,0,n1._i*sizeof(unsigned int));
|
|
res[char_offset] = bn_remainder(n2,0,0,10);
|
|
if(bn_smaller(n2,10)){
|
|
break;
|
|
}
|
|
char_offset++;
|
|
bn_division(n2,n1,0,0,10);
|
|
memset(n2._p,0,n2._i*sizeof(unsigned int));
|
|
}
|
|
free(n1._p);free(n2._p);
|
|
char* out = malloc(char_offset+2);
|
|
memset(out,0,char_offset+2);
|
|
for(int i=0;i<char_offset+1;i++){
|
|
out[i]='0'+res[char_offset-i];
|
|
}
|
|
free(res);
|
|
return out;
|
|
}
|
|
|
|
struct fn Chudnovsky(unsigned long _k){
|
|
|
|
}
|
|
|
|
int main(){
|
|
struct bn n1 = nn2bn(0xFFFFFFFFFFFFFFFFULL);
|
|
struct bn n2 = bn_add_nn(n1,&BN_ZERO,0,0xFFFFFFFFU);
|
|
char* out = bn2string(n2);
|
|
printf("%s",out);
|
|
free(out);
|
|
} |