找回密码
 欢迎注册
查看: 20279|回复: 17

[分享] 找一个含有你生日字串的回文素数

[复制链接]
发表于 2008-4-1 15:26:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
有帖子快速求出10^12以内回文素数
我突然想到,搜索一回文素数,使之含有自己身份证上的连续数字,说干就干。

写了一段代码:
  1. // 对应帖号: http://bbs.emath.ac.cn/viewthread.php?tid=284&page=1&fromuid=8#pid2303
  2. // 请复制并覆盖 HugeCalc 安装包解压后的 ansi_c++.cpp 文件,
  3. // 然后,修改你需要的字串,编译,运行。。。

  4. #include <iostream.h>

  5. #include "../../../HugeCalc_API/CppAPI/Include/HugeCalc.h"        // 公共接口
  6. #include "../../../HugeCalc_API/CppAPI/Include/HugeInt.h"        // 10进制系统

  7. #pragma message( "automatic link to ../../../HugeCalc_API/CppAPI/Lib/HugeCalc.lib" )
  8. #pragma comment( lib, "../../../HugeCalc_API/CppAPI/Lib/HugeCalc.lib" )


  9. int main(int argc, char* argv[])
  10. {
  11.         cout << "Call " << HugeCalc::GetVer() << endl;

  12.         if ( HC_LICENSE_NONE == HugeCalc::GetLicenseLevel())
  13.         {
  14.                 cout << endl << "警告:您未通过 HugeCalc.dll 的许可认证!" \
  15.                         << endl << endl << "解决方案可选下列方案之一:" \
  16.                         << endl <<        "    一、请将本程序移动到“/CopyrightByGuoXianqiang/[../]”目录下运行;" \
  17.                         << endl <<        "    二、或请在 HugeCalc.chm 中进行注册(一劳永逸)。" \
  18.                         << endl << endl;
  19.                 system( "pause" );
  20.                 return (-1);
  21.         }

  22.         const CHAR chNum[] = "33:31 52-21-7002";        // 含有目标数字字串
  23.         const CHAR chRev[] = "2007-12-25 13:33";        // 反序字串

  24.         CHugeInt hugeHead(chNum);
  25.         CHugeInt hugeTail(chRev);        // 请确保末位为 1、3、7、9 之一
  26.         CHugeInt hugeMean;
  27.         CHugeInt hugeTry;
  28.         const UINT32 mod3 = hugeTail % 3;
  29.         const UINT32 d = hugeTail.GetDigits();

  30.         // 初始化
  31.         HugeCalc::EnableTimer( TRUE );
  32.         HugeCalc::ResetTimer();

  33.         for ( UINT32 m = 0; m <= 9; ++m )
  34.         {
  35.                 hugeHead = chNum;

  36.                 // 避免:hugeTry 始终为合数情形
  37.                 if ( 0 == m && hugeTry.Gcd( hugeHead, hugeTail ) != 1 )
  38.                 {
  39.                         ++m;
  40.                 }

  41.                 // 防止:hugeTry 始终能被3整除情形
  42.                 if ( ( mod3 * 2 + ( m % 3 )) % 3 == 0 \
  43.                         && ++m > 9 )
  44.                 {
  45.                         break;
  46.                 }
  47.                 ( hugeMean = m );

  48.                 hugeMean.DecLShift( d );
  49.                 hugeHead.DecLShift( d + 1 );

  50.                 while( !((( hugeTry = hugeHead ) += hugeMean ) += hugeTail ).IsPrime())
  51.                 {
  52.                         hugeMean *= 10;
  53.                         hugeHead *= 100;
  54.                 }

  55.                 cout << endl << "m = " << m << "\t" << hugeTry.GetStr( FS_NORMAL ) \
  56.                         << "\t(" << hugeTry.GetDigits() << " digits)" << endl;
  57.         }

  58.         HugeCalc::EnableTimer( FALSE );

  59.         cout << endl << "耗时为:" << HugeCalc::GetTimerStr( FT_DOT06SEC_s ) << endl << endl;

  60.         system( "pause" );

  61.         return 0;
  62. }
复制代码
运行结果如下:
Call HugeCalc V8.0.0.0

m = 1   3331522170020000000000000000000000001000000000000000000000000200712251333       (73 digits)

m = 3   33315221700200000000000300000000000200712251333 (47 digits)

m = 4   3331522170024200712251333       (25 digits)

m = 6   333152217002000000000060000000000200712251333   (45 digits)

m = 7   3331522170020000007000000200712251333   (37 digits)

m = 9   333152217002090200712251333     (27 digits)

耗时为:0.007508 s

请按任意键继续. . .

备注:本论坛重建于 2007-12-25 13:33,所以特拿该字串为特征字串为例。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-4-1 16:26:11 | 显示全部楼层
73位那个不一定是素数
不是素数的可能是0.01%

我看能找到证明工具和分解工具么?
应该是无法分解的
=============================
全都瞬间判定可能是素数 :)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-4-1 16:30:34 | 显示全部楼层
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-4-1 19:19:33 | 显示全部楼层
HugeCalc 的 IsPrime() 是可信的。

对程序常量字符串略作修改,得到如下结果:
Call HugeCalc V8.0.0.0

m = 1   1234567890000001000000987654321 (31 digits)

m = 2   123456789000020000987654321     (27 digits)

m = 4   1234567894987654321     (19 digits)

m = 5   123456789000000000000000000050000000000000000000987654321       (57 digits)

m = 7   12345678900700987654321 (23 digits)

m = 8   12345678900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000987654321 (767 digits)

耗时为:7.114510 s

请按任意键继续. . .
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-4-1 20:08:31 | 显示全部楼层
我用的那个程序是基于雅克比和测试的
具体原理俺不清楚,因为没代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-11-26 19:32:11 | 显示全部楼层

用mathematica进行计算的代码!

Do[a = 333152217002*10^n + m*10^((n + 11)/2) + 200712251333;
If[n > 12 && PrimeQ[a], Print[{a, m, n + 12}]], {m, 1, 9}, {n, 1, 80,
   2}]
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-11-26 19:34:20 | 显示全部楼层

其结果

{333152217002000000000000000000000000100000000000000000000000020071225\
1333,1,73}
{33315221700200000000000300000000000200712251333,3,47}
{3331522170024200712251333,4,25}
{333152217002000000000060000000000200712251333,6,45}
{333152217002000000000000000000000060000000000000000000000200712251333\
,6,69}
{3331522170020000007000000200712251333,7,37}
{333152217002000000000000000000000000700000000000000000000000020071225\
1333,7,73}
{333152217002090200712251333,9,27}

结果说明:其格式为{回文素数,中间的那个数字,位数}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-11-26 19:52:10 | 显示全部楼层

疑问!

郭先强先生的那个代码真长,以至于我都不想看一下,虽然有注释。
不知道我的输出结果为什么比郭先强多出两个,疑惑中...........
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-11-26 19:55:08 | 显示全部楼层

再来一个!

{123456789000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000004000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
000000000000000987654321,4,1002}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-11-26 19:56:45 | 显示全部楼层

忘记加代码了!

Do[a = 123456789*10^n + m*10^((n + 8)/2) + 987654321;
   If[PrimeQ[a], Print[{a, m, n + 8}]], {m, 1, 9}, {n, 9, 1000}]
输出结果有些长,所以就不输出了!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-12-22 11:58 , Processed in 0.026729 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表