prog.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 08 August 2019
  4. # https://github.com/trizen
  5. # !!! UPDATE !!! a(5) does NOT exist!
  6. # https://www.primepuzzles.net/puzzles/puzz_970.htm
  7. # Sequence by Michel Marcus, Aug 08 2019:
  8. # a(n) is the least prime that can be written as a sequence of primes separated by n single zeros, and where every 0-splitting is prime.
  9. # OEIS:
  10. # https://oeis.org/A309566
  11. # a(n) = {307, 130307, 309370307, ...}
  12. # Example:
  13. # 130307 is a term since 3, 7, 13, 307, 1303, 130307 are all prime.
  14. # Upper-bound for a(4):
  15. # a(4) <= 30281172370306703
  16. # In 09 August 2019, Giovanni Resta confirmed:
  17. # a(4) = 30281172370306703
  18. # Examples for a(4):
  19. # 30281172370306703, 301594362103085903, 303761778310306703, 304443276703085903, 305939303094354103, 3011778479030992903, 3011896539670306703, 3012927194890306703, 3014963894903097103, 3016816935703093703, 3023139498103093703, 3026588214703075703, 3032246286703075703, 3035470309252484903, 3037764428290306703, 3038473344190306703, 3039932330304465103, 3044821843103097103, 3049368719030358103, 3049985459770306703, 3057445198903075703, 3061847197030993103, 3064846888303093703, 3067030183974572303, 3067030332163394903, 3067030348527266903, 3067030478639642303, 3067030497473248703, 3067030997681711303, 3067645533103075703, 3072491497703097103, 3072749526103085903, 3073889590305544103, 3073963252903093703, 3075577030113527903, 3078489641030718703, 3084581832490306703, 3084649030419787703, 3086251985903097103, 3093183836530306703, 3093183838103097103, 30124737689030718703, 30183614870309146303, 30195833413030354703, 30228888229303075703, 30265851669103075703, 30465664615303075703, 30515363257303075703, 30543121279903075703, 30552963618703075703, 30593930303461578103, 30595584988303075703, 30658392925903075703, 30665897679103075703, 30792830304538443103, 30859030371371987303, 30859030593194365703, 30859030712278822103, 30861769663030354703, 30877390305379612103, 30937030215236599703, 30937030367883386303, 30937030415566234103, 30937030461799766903, 30937030568452513103, 30937030635824981903, 30937030724629916303, 30939861431030992903, 30971030354345164903, 30971030684898898903, 30971030891554947103, 30971030971751481703, 30971030992452727303, 301111195733030358103, 301155918319030354703, 301177237253030227303, 301182958529030899903, 301457243267030899903, 301895697247030993103, 301954662769030993103, 301968654439030354703, 302145543341030752903, 302178129677030718703, 302273030783455968703, 302273030798864794303, 302273030943448723303, 302273030951867635903, 302295824591030358103, 302447371181030718703, 302518794379030993103, 302531921143030354703, 302546383691030745103, 302664968561030718703, 302987953469030899903, 303149392669030993103, 303547030232725648703, 303547030256915137103, 303547030624198963103, 303547030753956287303, 303547030819182295703, 303581030216517976903, 303581030225361652303, 303581030624539152703, 303581030693888175103, 303581030739547761103, 303625387553030668903, 303743375543030745103, 303819438431030853703, 304185898931030853703, 304412629751030992903, 304497569459030745103, 304582316261030745103, 304612953677030227303, 304648733281030993103, 304685654327030962903, 304777645351030354703, 304955262413030718703, 304956514699030993103, 305372856263030992903, 305623794551030752903, 305999166773030992903, 306526556789030962903, 306654259489030354703, 306689030353582242703, 306689030788674993103, 306689030924845335103, 306986548411030354703, 307187030298754279903, 307187030596887453703, 307187030792675985103, 307187030817836769703, 307451030117887267903, 307451030414578392703, 307451030441367118303, 307451030674636122103, 307529030179968442903, 307967599427030752903, 308189735963030718703, 308415448829030358103, 308458618691030745103, 308537030244192331303, 308537030289217692103, 308537030638276218703, 308537030686626996703, 308537030728574626903, 308537030817544923703, 308537030964448899103, 308982713933030668903, 308999030355783949903, 308999030479684449703, 308999030561545797103, 308999030698945558903, 308999030773651467103, 309399351769030993103, 309629030485525183903, 309629030984544487903, 309913134263030853703, 309929030798191633903, 309929030822141634103, 309931030269443182703, 309931030323235967903, 309931030571152946303, 309931030624229551703, 309991984789030354703
  20. # The n-th term contains binomial(n+2, n) zero-splitting primes (including itself), not necessary distinct.
  21. # The 10 zero-splitting primes in 309370307 are: [3, 3, 7, 307, 937, 30937, 93703, 3093703, 9370307, 309370307]
  22. # The 15 zero-splitting primes in 30281172370306703 are: [3, 3, 3, 67, 3067, 6703, 306703, 28117237, 2811723703, 3028117237, 302811723703, 2811723703067, 281172370306703, 302811723703067, 30281172370306703]
  23. # The term a(5), which is currently unknown, would contain 21 zero-splitting primes.
  24. use 5.020;
  25. use ntheory qw(:all);
  26. use experimental qw(signatures);
  27. sub isok ($p) {
  28. is_prob_prime($p) || return;
  29. my @P = split('0', $p);
  30. my $end = $#P;
  31. foreach my $i (0 .. $end - 1) {
  32. foreach my $j ($i + 1 .. $end) {
  33. is_prob_prime(join('0', @P[$i .. $j])) || return;
  34. }
  35. }
  36. vecall { is_prob_prime($_) } @P;
  37. }
  38. # These numbers are OK
  39. foreach my $p (
  40. qw(
  41. 307 130307 309370307 3067030853 13903093703 22303075703
  42. 30369101303 30670301531 30670302287 30670308887 30752903023 30859030313
  43. 30281172370306703 301594362103085903 303761778310306703 304443276703085903
  44. 305939303094354103 3011778479030992903 3011896539670306703 3012927194890306703
  45. 3014963894903097103 3016816935703093703 3023139498103093703 3026588214703075703
  46. 3032246286703075703 3035470309252484903 3037764428290306703 3038473344190306703
  47. 3039932330304465103 3044821843103097103 3049368719030358103 3049985459770306703
  48. 3057445198903075703 3061847197030993103 3064846888303093703 3067030183974572303
  49. 3067030332163394903 3067030348527266903 3067030478639642303 3067030497473248703
  50. 3067030997681711303 3067645533103075703 3072491497703097103 3072749526103085903
  51. 3073889590305544103 3073963252903093703 3075577030113527903 3078489641030718703
  52. 3084581832490306703 3084649030419787703 3086251985903097103 3093183836530306703
  53. 3093183838103097103 30124737689030718703 30183614870309146303 30195833413030354703
  54. 30228888229303075703 30265851669103075703 30465664615303075703 30515363257303075703
  55. 30543121279903075703 30552963618703075703 30593930303461578103 30595584988303075703
  56. 30658392925903075703 30665897679103075703 30792830304538443103 30859030371371987303
  57. 30859030593194365703 30859030712278822103 30861769663030354703 30877390305379612103
  58. 30937030215236599703 30937030367883386303 30937030415566234103 30937030461799766903
  59. 30937030568452513103 30937030635824981903 30937030724629916303 30939861431030992903
  60. 30971030354345164903 30971030684898898903 30971030891554947103 30971030971751481703
  61. 30971030992452727303 301111195733030358103 301155918319030354703 301177237253030227303
  62. 301182958529030899903 301457243267030899903 301895697247030993103 301954662769030993103
  63. 301968654439030354703 302145543341030752903 302178129677030718703 302273030783455968703
  64. 302273030798864794303 302273030943448723303 302273030951867635903 302295824591030358103
  65. 302447371181030718703 302518794379030993103 302531921143030354703 302546383691030745103
  66. 302664968561030718703 302987953469030899903 303149392669030993103 303547030232725648703
  67. 303547030256915137103 303547030624198963103 303547030753956287303 303547030819182295703
  68. 303581030216517976903 303581030225361652303 303581030624539152703 303581030693888175103
  69. 303581030739547761103 303625387553030668903 303743375543030745103 303819438431030853703
  70. 304185898931030853703 304412629751030992903 304497569459030745103 304582316261030745103
  71. 304612953677030227303 304648733281030993103 304685654327030962903 304777645351030354703
  72. 304955262413030718703 304956514699030993103 305372856263030992903 305623794551030752903
  73. 305999166773030992903 306526556789030962903 306654259489030354703 306689030353582242703
  74. 306689030788674993103 306689030924845335103 306986548411030354703 307187030298754279903
  75. 307187030596887453703 307187030792675985103 307187030817836769703 307451030117887267903
  76. 307451030414578392703 307451030441367118303 307451030674636122103 307529030179968442903
  77. 307967599427030752903 308189735963030718703 308415448829030358103 308458618691030745103
  78. 308537030244192331303 308537030289217692103 308537030638276218703 308537030686626996703
  79. 308537030728574626903 308537030817544923703 308537030964448899103 308982713933030668903
  80. 308999030355783949903 308999030479684449703 308999030561545797103 308999030698945558903
  81. 308999030773651467103 309399351769030993103 309629030485525183903 309629030984544487903
  82. 309913134263030853703 309929030798191633903 309929030822141634103 309931030269443182703
  83. 309931030323235967903 309931030571152946303 309931030624229551703 309991984789030354703
  84. )
  85. ) {
  86. isok($p) || die "false-negative: $p";
  87. }
  88. # These numbers are NOT OK
  89. foreach my $p (
  90. qw(
  91. 13030307
  92. 202030207
  93. 302030203
  94. 30937030704161
  95. 3074510309146303
  96. 30143088130306703
  97. 30174190301812103
  98. )
  99. ) {
  100. isok($p) && die "false-positive: $p";
  101. }
  102. my @primes = grep { $_ != 2 } grep { $_ != 5 } grep { !/0/ } @{primes(1e4)};
  103. sub generate_from_prefix ($root, $k) {
  104. #~ return if ($root >= 30281172370306703);
  105. say "k = $k -> $root" if ($k >= 4);
  106. if ($k >= 5) {
  107. die "Found: $root";
  108. }
  109. foreach my $p (@primes) {
  110. my $x = join('0', $root, $p);
  111. if (isok($x)) {
  112. __SUB__->($x, $k + 1);
  113. }
  114. }
  115. }
  116. sub generate_from_suffix ($root, $k) {
  117. #~ return if ($root >= 30281172370306703);
  118. say "k = $k -> $root" if ($k >= 4);
  119. if ($k >= 5) {
  120. die "Found: $root";
  121. }
  122. foreach my $p (@primes) {
  123. my $x = join('0', $p, $root);
  124. if (isok($x)) {
  125. __SUB__->($x, $k + 1);
  126. }
  127. }
  128. }
  129. # Look for special form "30P030Q03", which gives upper-bounds for a(4), where P and Q are primes.
  130. sub special_form_search {
  131. my @primes_p = @{primes(1e7, 1e7 + 1e6)}; #@{primes(1e5)};
  132. my @primes_q = @{primes(1e4)};
  133. foreach my $p (@primes_p) {
  134. say "Testing: $p";
  135. foreach my $q (@primes_q) {
  136. my $t = "30${p}030${q}03";
  137. if ($t >= 30281172370306703) {
  138. last;
  139. }
  140. is_prime("30${p}") || next;
  141. is_prime("${q}03") || next;
  142. is_prime("30${q}03") || next;
  143. is_prime("${p}030${q}") || next;
  144. is_prime("30${p}030${q}") || next;
  145. is_prime("${p}030${q}03") || next;
  146. is_prime($t) || next;
  147. if (isok($t)) {
  148. die("Found: $t");
  149. }
  150. }
  151. }
  152. }
  153. #special_form_search();
  154. sub exaustive_search {
  155. foreach my $p (@primes) {
  156. say ":: Generating from suffix = $p";
  157. generate_from_suffix($p, 0);
  158. }
  159. }
  160. #exaustive_search();
  161. sub generate_from_large_primes { # generate from large prime prefix and suffix
  162. forprimes {
  163. #if (/0/ and not /00/) {
  164. if (!/0/) {
  165. #~ if (vecall { is_prime($_) } split('0', $_)) {
  166. #~ my $count = () = /0/g;
  167. my $count = 0;
  168. #~ if ($count >= 4) {
  169. #~ die "Found: $_" if isok($_);
  170. #~ }
  171. #generate_from_prefix($_, $count) if ($count < 3);
  172. #generate_from_suffix($_, $count) if ($count < 3);
  173. my $t = '30' . $_ . '03';
  174. if ($count < 2) {
  175. if (is_prob_prime('30' . $_) and is_prob_prime($_ . '03') and is_prob_prime($t)) {
  176. #say "Generating from suffix = $t";
  177. generate_from_prefix($t, $count + 2);
  178. generate_from_suffix($t, $count + 2);
  179. }
  180. }
  181. #~ }
  182. }
  183. }
  184. 1e7, 1e8;
  185. }
  186. generate_from_large_primes();
  187. __END__
  188. Examples with 3-zeros, for primes < 5000:
  189. k = 3 -> 30369101303
  190. k = 3 -> 35710306703
  191. k = 3 -> 46630306703
  192. k = 3 -> 22303075703
  193. k = 3 -> 369703085903
  194. k = 3 -> 13903093703
  195. k = 3 -> 96703093703
  196. k = 3 -> 186103093703
  197. k = 3 -> 61703097103
  198. k = 3 -> 3036910175303
  199. k = 3 -> 2531030227303
  200. k = 3 -> 2789030227303
  201. k = 3 -> 304670265703
  202. k = 3 -> 3028430286103
  203. k = 3 -> 3022970292703
  204. k = 3 -> 301390388903
  205. k = 3 -> 3045610388903
  206. k = 3 -> 3025790421103
  207. k = 3 -> 309370451903
  208. k = 3 -> 309370307
  209. k = 3 -> 30859030313
  210. k = 3 -> 3067030853
  211. k = 3 -> 3035810301493
  212. k = 3 -> 30670301531
  213. k = 3 -> 307570301867
  214. k = 3 -> 308590301933
  215. k = 3 -> 30670302287
  216. k = 3 -> 3022730302663