static int div = 1;
for (;div <= cal; ++div)
{
int quo = 0;
int rem = 0;
for (int j = 0; j< length; ++j)
{
quo = rem * 10 + lastTmp[j];
rem = quo % div;
quo = quo / div;
tmp[j] = quo;
if (firstNoZero == 0 && div == cal && tmp[j] != 0)
{
firstNoZero = j;
}
}
}
div = cal;
int flag = 0;
if (firstNoZero == 0) // All zero
{
break; // no need to calculate anymore
}
for (int i = length - 1; i >= firstNoZero - 1; --i)
{
int tmp_sum = tmp[i] + result[i] + flag;
flag = (tmp_sum >= 10 ? 1 : 0);
result[i] = (flag ? tmp_sum - 10 : tmp_sum);
}
int main(void)
{
int bit;
char *data,*dataed;
int *rank;
printf("Input the bit: ");
scanf("%d",&bit);
rank=(int*)malloc(sizeof(int)*bit);
data=(char*)malloc(sizeof(char)*bit);
dataed=(char*)malloc(sizeof(char)*bit);
memset((void*)dataed,0,bit);
printf("Please wait...");
for (int i=0;i<bit;i++)
{
reciprocal(data,bit,i+1);
add(data,dataed,bit);
}
dataed[0]=dataed[0]+1;
printf("\nThe E is: ");
for (int i=0;i<bit;i++)
{
if (i==1)
printf(".");
printf("%d",dataed[i]);
if (i+1%2000==0)
system("pause");
}
free(rank);
free(data);
free(dataed);
system("pause");
return 0;
}
void reciprocal(char *data,int bit,int num)
{
int quotient,remainder;
memset(data,0,bit);
*data=1;
for (int i=1;i<=num;i++)
{
remainder=0;
for (int k=0;k<bit;k++)
{
quotient=remainder*10+data[k];
remainder=quotient%i;
quotient=quotient/i;
data[k]=quotient;
}
}
}
void add(char* data,char* dataed,int bit)
{
int flag=0,b_flag;
for (int i=bit-1;i>=0;i--)
{
if (data[i]+dataed[i]+flag>=10)
b_flag=1;
else
b_flag=0;
dataed[i]=(dataed[i]+data[i]+flag)%10;
flag=b_flag;
}
}
for (int cal = 2;cal <= length; ++cal)
{
memset(tmp,0,length);
int firstNoZero = 0;
//static int div = 1; 放在循环外部声明
for (;div <= cal; ++div)
{
int quo = 0;
int rem = 0;
for (int j = 0; j< length; ++j)
{
quo = rem * 10 + lastTmp[j];
rem = quo % div;
quo = quo / div;
tmp[j] = quo;
if (firstNoZero == 0 && div == cal && tmp[j] != 0)
firstNoZero = j;
}
}
div = cal;
int flag = 0;
if (firstNoZero == 0) // All zero
break; // no need to calculate anymore
//for (int i = length - 1; i >= firstNoZero - 1; --i) 进位错,修改如下:
for (int i = length - 1; i >= firstNoZero - 2; --i)
{
int tmp_sum = tmp[i] + result[i] + flag;
flag = (tmp_sum >= 10 ? 1 : 0);
result[i] = (flag ? tmp_sum - 10 : tmp_sum);
}