1
Name:
Anonymous
2011-08-15 7:48
I'm trying to write a HugeMotherFuckingInt processing library in C.
My first try, I stored the numbers as a char[] and computed them using basic arithmetic.
My second try, I stored the numbers as unsigned long char[]. This proved to be a faster, easier and clearer approach.
Still though, it seems to be a bit too slow.
What other data structures might result in faster execution of basic arithmetic +/*-?
Also, are there any algorithms to speed up the arithmetic operations themselves?
14
Name:
n3n7i
2011-08-15 22:45
//----------------------------------
void Add(int xa[], int xb[]){
int i, iCarry=0, raymax=0;
raymax = xa[RRay];
if(xb[RRay]>raymax) raymax = xb[RRay];
ac[RRay] = raymax;
for(i=0;i<=ac[RRay];i++){
ac[i] = xa[i] + xb[i] + iCarry;
iCarry=0;
[b]if(ac[i]>=CutOff){[/b] //<<Corrected
iCarry++;
ac[i]-=CutOff;
}
if((i==ac[RRay])+(iCarry==1)==2) ac[RRay]++;
}
}
//----------------------------------
void Sub(int xa[], int xb[]){
int i, iCarry=0, raymax=0;
raymax = xa[RRay];
if(xb[RRay]>raymax){ printf("Problem!"); raymax = xb[RRay]; }
ac[RRay] = raymax;
for(i=0;i<=ac[RRay];i++){
ac[i] = xa[i] - (xb[i] + iCarry);
iCarry=0;
if(ac[i]<0){
iCarry++;
ac[i]+=CutOff;
}
if((i==ac[RRay])+(iCarry==1)==2){ printf("Negative!"); ac[RRay]++; }
}
}
//-----------------------------------
15
Name:
n3n7i
2011-08-15 23:38
more...?
#include <stdio.h>
#include <stdlib.h>
const RRay = 50; // Max array Size //
const CutOff = 1000; // Base
int aa[52]; int ab[52]; int ac[52];
//----------------------------------------------------
void Set(int xa[], int xb[]){
int i;
for(i=0;i<=RRay;i++) xa[i] = xb[i];
}
//----------------------------------
int GOWFish(int a, int b){
int ret=0;
if(a!=b){
if(a>b) ret= 1;
if(a<b) ret=-1;
}
return ret;
}
//---------------------
int GreaterOrWhat(int xa[], int xb[]){
int a, b, i, ret=0;
a = xa[RRay];
b = xb[RRay];
i =a+1;
ret = GOWFish(a, b);
while((ret==0) + (i>=0)==2){
i--;
a = xa[i];
b = xb[i];
ret = GOWFish(a, b);
}
return ret;
}
//----------------------------------------------
void Add(int xa[], int xb[]){
int i, iCarry=0, raymax=0;
raymax = xa[RRay];
if(xb[RRay]>raymax) raymax = xb[RRay];
ac[RRay] = raymax;
for(i=0;i<=ac[RRay];i++){
ac[i] = xa[i] + xb[i] + iCarry;
iCarry=0;
if(ac[i]>=CutOff){
iCarry++;
ac[i]-=CutOff;
}
if((i==ac[RRay])+(iCarry==1)==2) ac[RRay]++;
}
}
//----------------------------------
void CheckRRay(int xa[]){
int i, iTop=0;
for(i=0;i<RRay;i++){
if(xa[i]>0) iTop=i;
}
if(xa[RRay]!=iTop){ printf("Fixed RRay"); xa[RRay]=iTop;}
}
//-------------------------------------
void Sub(int xa[], int xb[]){
int i, iCarry=0, raymax=0;
raymax = xa[RRay];
if(xb[RRay]>raymax){ printf("Problem!"); raymax = xb[RRay]; }
ac[RRay] = raymax;
for(i=0;i<=ac[RRay];i++){
ac[i] = xa[i] - (xb[i] + iCarry);
iCarry=0;
if(ac[i]<0){
iCarry++;
ac[i]+=CutOff;
}
if((i==ac[RRay])+(iCarry==1)==2){ printf("Negative!"); ac[RRay]++; }
}
CheckRRay(ac);
}
//-----------------------------------
void Entry(int x[]){
int i, i2, i3;
printf("Enter Base size"); //<<------- xRAY [Base length]
scanf("%i",&i3);
if(i3>RRay) i3 = RRay;
for(i=i3;i>=0;i--){
printf("Enter No i");
scanf("%i",&i2);
printf("Entered %i\n", i2);
x[i]=i2;
}
x[RRay] = i3; //<<------- xRAY [store current length]
for(i=i3;i>=0;i--) printf("%3i,", x[i]);
}
//------------------------------------
void Printout(int xX[]){
int i;
for(i=xX[RRay];i>=0;i--) printf("%3i,", xX[i]);
printf("\n");
}
//====================================
int main(){
Entry(aa);
Set(ac, aa);
Add(ac, ac);
printf("\n (* 2 =) \n");
Printout(ac);
Add(ac, ac);
printf("\n (* 2 =) \n");
Printout(ac);
Add(ac, ac);
printf("\n (* 2 =) \n");
Printout(ac);
printf("\n ( - ) \n aa : ");
Printout(aa);
Set(ab, ac);
Sub(ac, aa);
printf("\n ac : ");
Printout(ac);
printf("aa > ac?");
printf("%i", GreaterOrWhat(aa, ac));
printf("\nab > ac?");
printf("%i", GreaterOrWhat(ab, ac));
printf("\nab + ac\n");
Add(ab, ac);
Printout(ac);
return 0;
}