我完全照搬 mathe 3#的算法,只不过调用的是 HugeCalc 大数算法库,速度提升了好几个数量级!
(sqrt(2)-1)^r =sqrt(k) - sqrt(k-1)
代码如下:- #include < iostream.h >
-
- #include < HugeCalc.h > // 公共接口
- #include < HugeInt.h > // 10进制系统
- #include < HugeIntX.h > // 16进制系统
-
- #pragma message( "automatic link to .../HugeCalc_API/CppAPI/Lib/HugeCalc.lib" )
- #pragma comment( lib, "HugeCalc.lib" )
-
- int main(int argc, char* argv[])
- {
- cout << "Call " << HugeCalc::GetVer() << endl;
-
- if ( HC_LICENSE_NONE == HugeCalc::GetLicenseLevel() )
- {
- cout << "\r\n警告:您未通过 HugeCalc.dll 的许可认证!\r\n" \
- << "\r\n解决方案可选下列方案之一:" \
- << "\r\n 一、请将本程序移动到“/CopyrightByGuoXianqiang/[../]”目录下运行;" \
- << "\r\n 二、或请在 HugeCalc.chm 中进行注册(一劳永逸)。" \
- << endl;
- }
- else
- {
- CHugeInt a, b, k;
- UINT32 i, r;
-
- cout << "*** [sqrt(2)-1]^r = sqrt(k) - sqrt(k-1) ***" << endl;
- cout << "input r, output k ...(if r==0, then exit)" << endl;
-
- for ( ; ; )
- {
- cout << endl << "r = ";
- cin >> r;
-
- if ( 0 == r ) break;
-
- HugeCalc::EnableTimer();
- HugeCalc::ResetTimer();
-
- a = 1;
- b = -1;
-
- for( i=2; i<=r; ++i )
- {
- k = a;
- a.Swap(b) -= b;
- b.Swap(k) -= a;
- }
-
- if ( 0 == (1 & r) )
- {
- k.Swap(b).Pow(2);
- }
- else
- {
- k.Swap(a).Pow(2) *= 2;
- }
-
- HugeCalc::EnableTimer( FALSE );
-
- cout << "k = " << k.GetStr(FS_NORMAL) << endl;
- cout << "computation took " << HugeCalc::GetTimerStr() << endl;
- }
- }
-
- cout << endl;
- system( "pause" );
-
- return 0;
- }
复制代码 编译好的程序压缩包:
(sqrt(2)-1)^r.zip
(318.18 KB, 下载次数: 311)
(注:非 HugeCalc 注册用户也可正常运行) |