- 注册时间
- 2007-12-28
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 12785
- 在线时间
- 小时
|
发表于 2012-12-12 14:59:02
|
显示全部楼层
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "windows.h"
-
- typedef __int64 INT64;
-
- typedef unsigned long DWORD;
-
- #define DEC_RADIX 1000000000
-
- #define PRECISION_DIV9 4 //10进制数表示的数字个数除以9
-
-
- const DWORD PI_VALUE[][2]={
- {3,141592653},
- {31,415926535},
- {314,159265358},
- {3141,592653589},
- {31415,926535897},
- {314159,265358979},
- {3141592,653589793},
- {31415926,535897932},
- {314159265,358979323},
- };
-
- double scales[]=
- {
- 1.00,
- 10.00,
- 100.00,
- 1000.00,
- 10000.00,
- 100000.00,
- 1000000.00,
- 10000000.00,
- 100000000.00,
- };
-
-
- int Uint32BitCount(DWORD n)
- {
- _asm
- {
- bsr eax,n
- inc eax
- }
- }
-
- //******************************************************
- DWORD DEC_Array_Mul_DWORD_ALU(DWORD *a, size_t len, DWORD num)
- //******************************************************
- {
- static DWORD _s_TEN9=DEC_RADIX;
- //---------------------------
- _asm
- {
- push esi
-
- mov ecx,len
- mov esi,a
- lea esi,[esi+ecx*4-4]
- xor ecx,ecx //carry
-
- test esi,3 //the a must be 4byte align
- jnz thisExit
-
- jmp cmp000
-
- loop_start:
- mov eax,[esi]
- mul dword ptr num
- add eax,ecx
- adc edx,0
- div dword ptr _s_TEN9
- mov [esi],edx
- mov ecx,eax
- sub esi,4
- cmp000:
- cmp esi,a
- jae loop_start
- thisExit:
- mov eax,ecx
- pop esi
- }
- }
-
- double check_diff(DWORD num[],int shift)
- {
- int t,carry;
- DWORD tArr[2];
-
- tArr[0]=PI_VALUE[shift][0];
- tArr[1]=PI_VALUE[shift][1];
-
- t=tArr[1]-num[1];
- if (t<0)
- {
- carry=1;
- t+=DEC_RADIX;
- }
- else
- carry=0;
-
- tArr[1]=t;
-
- t=tArr[0]-num[0]-carry;
- tArr[0]=t;
-
- if (t<0)
- {
- return 999;
- }
- else
- {
- double diff;
- diff =tArr[0] + tArr[1] * 0.000000001;
- diff /= scales[shift];
- return diff;
-
- }
- }
-
- void print_number(DWORD n,DWORD res[],int len,double exp)
- {
- char buff[PRECISION_DIV9*9+9];
- char *p;
- int i,s;
- INT64 e;
-
- p=buff;
- p+=sprintf(p,"%d",res[0]);
- s=strlen(buff);
-
- if (len>1)
- {
- for (i=1;i<len;i++)
- p+=sprintf(p,"%09d",res[ i ]);
- }
-
- printf("\n%u!=",n);
- for (i=0;i<strlen(buff);i++)
- {
- printf("%c",buff[ i ]);
- if (i==0)
- printf(".");
- }
-
- e=(INT64)(exp+(len-1)*9+s-1);
- printf("*10^%I64d\n",e);
- }
-
-
- void search_n()
- {
- DWORD i,j;
- DWORD carry;
-
- int shift;
- int bc,fac_len; // fac(i)的结果的数组的长度
- double diff,min_diff;
- double res_exp;
-
- DWORD fac[PRECISION_DIV9+1]; //fac(i)的结果的数组
- DWORD time;
-
-
- time=GetTickCount();
- min_diff=1;
- res_exp=0;
- fac[0]=1;
- fac_len=1;
-
- i=1;
- //while (i<DEC_RADIX)
- while (i!=0)
- {
- carry=DEC_Array_Mul_DWORD_ALU(fac,fac_len,i);
- if (carry>=DEC_RADIX)
- {
- for (j=PRECISION_DIV9-2;j>=2;j--)
- fac[j]=fac[j-2];
- fac[0]=carry / DEC_RADIX;
- fac[1]=carry % DEC_RADIX;
-
- if ( fac_len+2 <= PRECISION_DIV9)
- fac_len+=2;
- else if (fac_len+1 ==PRECISION_DIV9)
- {
- fac_len=PRECISION_DIV9;
- res_exp+=9;
- }
- else
- res_exp+=18;
- }
- else if (carry>0)
- {
- for (j=PRECISION_DIV9-1;j>=1;j--)
- fac[j]=fac[j-1];
- fac[0]=carry;
- if (fac_len<PRECISION_DIV9)
- fac_len++;
- else
- res_exp+=9;
- }
- bc=Uint32BitCount(fac[0]);
-
- switch (bc)
- {
- case 2:
- shift=0;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 5:
- shift=1;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 9:
- shift=2;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 12:
- shift=3;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 15:
- shift=4;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 19:
- shift=5;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 22:
- shift=6;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 25:
- shift=7;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- case 29:
- shift=8;
- diff=check_diff(fac,shift);
- if (diff<min_diff)
- {
- min_diff=diff;
- if (fac_len>=2)
- print_number(i,fac,fac_len,res_exp);
- }
- break;
- default:
- break;
- }
- i++;
- }
-
- time=(GetTickCount()-time);
- printf("It took %d secs\n",time/1000);
-
- }
-
- int main(int argc, char* argv[])
- {
- search_n();
- return 0;
- }
复制代码 |
|