- 注册时间
- 2007-12-26
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 92615
- 在线时间
- 小时
|
发表于 2008-2-14 13:57:19
|
显示全部楼层
备注:本帖于 2008-02-15 19:30 最后修正!
参考 mathe 的代码,略作了些修改(省却了小素数的搜索过程):- // HugeCalcDemo.cpp : Defines the entry point for the console application.
- //
-
- // Project -> Setting -> C/C++ -> Code Generation --> Use run-time library:
- // Win32 Debug: Debug Multithreaded DLL
- // Win32 Release: Multithreaded DLL
-
- // 以下代码源自 mathe 的,略有修改
-
- #include <time.h>
- #include <iostream>
- using namespace std;
- #include "../../../HugeCalc_API/CppAPI/Include/HugeCalc.h" // 公共接口
- #include "../../../HugeCalc_API/CppAPI/Include/HugeInt.h" // 10进制系统
- #include "../../../HugeCalc_API/CppAPI/Include/HugeIntX.h" // 16进制系统
-
- #pragma message( "automatic link to ../../../HugeCalc_API/CppAPI/Lib/HugeCalc.lib" )
- #pragma comment( lib, "../../../HugeCalc_API/CppAPI/Lib/HugeCalc.lib" )
-
- #if 1
- #define _Test_HI
- #define integer CHugeInt
- #else
- #define _Test_HX
- #define integer CHugeIntX
- #endif
-
- #define SMALLPRIMES 0x1000000
-
- #define LIMIT_SIZE 10000000UL
- #define RANGE (LIMIT_SIZE<<5)
- UINT32 mask[LIMIT_SIZE];
- #define SET_MASK(x) (mask[(x)>>5] |= 1UL<<((x)&31))
- #define IS_SET(k,i) (mask[k]&(1UL<<(i)))
-
- int main(int argc, char* argv[])
- {
- cout << "Call " << HugeCalc::GetVer() << endl << endl;
-
- if ( HC_LICENSE_NONE == HugeCalc::GetLicenseLevel())
- {
- cout << endl << "警告:您未通过 HugeCalc.dll 的许可认证!" \
- << endl << endl << "解决方案可选下列方案之一:" \
- << endl << " 一、请将本程序移动到“/CopyrightByGuoXianqiang/[../]”目录下运行;" \
- << endl << " 二、或请在 HugeCalc.chm 中进行注册(一劳永逸)。" \
- << endl << endl;
- system( "pause" );
- return (-1);
- }
-
- // 初始化
- HugeCalc::EnableTimer( TRUE );
- HugeCalc::ResetTimer();
-
- // 下面这条语句可以不必调用,HugeCalc 初始化时内部已执行该过程
- // HugeCalc::SeedRandom(time(NULL));
-
- integer hugeStart;
- #ifdef _Test_HI
- #if 0
- hugeStart.Random(100);
- #else
- ( hugeStart = 1 ).DecLShift( 100 );
- #endif
- #else
- hugeStart.Random(335);
- #endif
-
- CONST UINT32 spcount = HugeCalc::GetPrimePi( SMALLPRIMES ) - 3;//skip prime 2,3,5
- UINT32 * sptable = new UINT32[ spcount ];
- HugeCalc::GetPrimeList( sptable, spcount, 7, SMALLPRIMES );
-
- UINT32 i,k,m,s,p,v,h;
- CONST UINT32 filter_list[] = { 0, 2, 6, 8 };
-
- memset( mask, 0, sizeof(mask) );
-
- m = hugeStart%30;
- hugeStart += (11>=m) ? (11-m) : (41-m); //After the normalization, hugeStart%30==11
-
- for(k=0;k<spcount;k++){
- p = sptable[k];
- m = hugeStart%p;
- v = HugeCalc::InvertMod(30,p);
- for(i=0;i<sizeof(filter_list)/sizeof(filter_list[0]);i++){
- s = filter_list[i]+m;
- s = ( p >= s ) ? ( p - s ) : ( p*2 - s );
- #if ( SMALLPRIMES <= 0xFFFF )
- ( s *= v ) %= p; //hugeStart+s*30+filter_list[i]==0 (mod p)
- #else
- s = (UINT32)( UInt32x32To64( s, v ) % p ); // 同上
- #endif
- for(h=s;h<RANGE;h+=p){
- SET_MASK(h);//Filter hugeStart+h*30
- }
- }
- }
-
- delete []sptable;
-
- cout << "Filter cost "<< HugeCalc::GetTimer( TIMER_UNIT_ms ) << "ms" <<endl;
- HugeCalc::ResetTimer();
-
-
- UINT32 tcount=0, gcount=0;
-
- s = 0;
- for(k=0;k<LIMIT_SIZE;k++){
- for(i=0;i<32;i++){
- if(!IS_SET(k,i)){//hugeStart+i*30 may be a candidate
- ++tcount;
- hugeStart += s;
- if( (s =0, hugeStart.TestPrimality(1)!=COMPOSITE ) &&
- (s-=2, (hugeStart+=2).TestPrimality(2)!=COMPOSITE) &&
- (s-=4, (hugeStart+=4).TestPrimality(3)!=COMPOSITE) &&
- (s-=2, (hugeStart+=2).IsPrime()) &&
- (s+=2, (hugeStart-=2).IsPrime()) &&
- (s+=4, (hugeStart-=4).IsPrime()) &&
- (s+=2, (hugeStart-=2).IsPrime())){
- //Output the candidate;
- cout<< HugeCalc::GetTimerStr(FT_HHMMSS_ms) << " No." << ++gcount \
- << " Found (n,n+2,n+6,n+8) where n is "
- #ifdef _Test_HI
- << hugeStart.GetStr( FS_NORMAL ) <<endl;
- #else // 下面语句可将16进制大数直接以10进制数格式输出
- << hugeStart.GetStrRadix( 10, NULL, FS_NORMAL ) <<endl;
- #endif
- }
- }
-
- s += 30;
- }
- }
-
- cout << "Total test "<<tcount<<" Numbers, get "<<gcount<<" numbers"<<endl;
-
- system( "pause" );
- return 0;
- }
复制代码 大家可以将它复制覆盖原 \HugeCalc\testDLL\src\ANSI_C++\ansi_c++.cpp,再编译即可。
在我的蹩脚机器上运行结果如下:- Call HugeCalc V8.0.0.0
-
- Filter cost 68254ms
- 00:00:07.194 No.1 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000001053594241
- 00:00:07.370 No.2 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000001069468231
- 00:00:16.808 No.3 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000002439435571
- 00:00:23.487 No.4 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000003413496841
- 00:00:25.334 No.5 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000003681511741
- 00:00:31.152 No.6 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000004552552081
- 00:00:37.123 No.7 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000005435223751
- 00:00:38.937 No.8 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000005692544401
- 00:00:44.441 No.9 Found (n,n+2,n+6,n+8) where n is 100000000000000000000000000
- 00000000000000000000000000000000000000000000000000000000000000006496820491
- 00:00:55.114 No.10 Found (n,n+2,n+6,n+8) where n is 10000000000000000000000000
- 000000000000000000000000000000000000000000000000000000000000000008049785491
- 00:00:57.931 No.11 Found (n,n+2,n+6,n+8) where n is 10000000000000000000000000
- 000000000000000000000000000000000000000000000000000000000000000008455204681
- 00:00:58.478 No.12 Found (n,n+2,n+6,n+8) where n is 10000000000000000000000000
- 000000000000000000000000000000000000000000000000000000000000000008532272461
- 00:01:02.213 No.13 Found (n,n+2,n+6,n+8) where n is 10000000000000000000000000
- 000000000000000000000000000000000000000000000000000000000000000009077484631
- Total test 51248 Numbers, get 13 numbers
- 请按任意键继续. . .
复制代码 速度快得惊人!
截屏如下:
四生素数
|
|