v1.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/usr/bin/perl
  2. # a(n) is the smallest prime number whose a056240-type is n (see Comments).
  3. # https://oeis.org/A293652
  4. use 5.014;
  5. #use Math::AnyNum qw(:overload);
  6. use ntheory qw(is_prime is_square_free next_prime vecprod prev_prime vecmin vecmax prime_count vecsum forprimes forcomposites factor lastfor forpart formultiperm);
  7. use experimental qw(signatures);
  8. #{forprime(p=5, , ip = primepi(p); if (ip > n, x = scompo(p); fmax = vecmax(factor(x)[, 1]); ifmax = primepi(fmax); if (ip - ifmax == n, y = fmax*snumbr(p - fmax; ); if (y == x, return (p); ); ); ); ); }
  9. #~ isok(k, n) = my(f=factor(k)); sum(j=1, #f~, f[j, 1]*f[j, 2]) == n;
  10. #~ snumbr(n) = my(k=2); while(!isok(k, n), k++); k; /* A056240 */
  11. #~ scompo(n) = forcomposite(k=4, , if (isok(k, n), return(k))); /* A288814 */
  12. #~ a(n) = {forprime(p=5, , ip = primepi(p); if (
  13. #~ ip > n, x = scompo(p); fmax = vecmax(factor(x)[, 1]); ifmax = primepi(fmax); if (ip - ifmax == n, y = fmax*snumbr(p - fmax; ); if (y == x, return (p); ); ); ); ); }
  14. #sub isok ($k, $n) {
  15. #my @f = factor($k);
  16. #vecsum(factor($k)) == $n;
  17. #}
  18. sub find_partitions ($diff) {
  19. my @part;
  20. forpart {
  21. #say "@_";
  22. #my $sum = vecsum(@_);
  23. #if ($p+$sum == $n) {
  24. #}
  25. push @part, [@_];
  26. } $diff, {n => 2, prime => 1};
  27. return @part;
  28. }
  29. my @table;
  30. my $last_value = 1;
  31. sub snumbr ($n) {
  32. if (exists $table[$n]) {
  33. return $table[$n];
  34. }
  35. for (my $k = 2; ;++$k) {
  36. my $sum = vecsum(factor($k));
  37. if ($sum == $k) {
  38. #say "Found k = $k";
  39. return $k;
  40. }
  41. }
  42. }
  43. sub scompo2 ($n) {
  44. for (my $k = $last_value ; ; ++$k) {
  45. next if is_prime($k);
  46. my $sum = vecsum(factor($k));
  47. if (exists $table[$sum]) {
  48. if ($table[$sum] > $k) {
  49. $table[$sum] = $k;
  50. }
  51. }
  52. else {
  53. $table[$sum] = $k;
  54. }
  55. if ($sum == $n) {
  56. #say "n=$n -> found: $k";
  57. say "Found: n=$n with $k = {", join(', ', factor($k)), "}";
  58. $last_value = $k + 1;
  59. return $k;
  60. }
  61. }
  62. }
  63. sub scompo ($n) {
  64. #my $k = 2;
  65. # return vecmin(
  66. if (exists $table[$n]) {
  67. say "Found n=$n with $table[$n] = {", join(', ', factor($table[$n])), "}";
  68. return $table[$n];
  69. }
  70. for (my $k = $last_value ; ; ++$k) {
  71. next if is_prime($k);
  72. my $sum = vecsum(factor($k));
  73. if (exists $table[$sum]) {
  74. if ($table[$sum] > $k) {
  75. $table[$sum] = $k;
  76. }
  77. }
  78. else {
  79. $table[$sum] = $k;
  80. }
  81. if ($sum == $n) {
  82. #say "n=$n -> found: $k";
  83. say "Found: n=$n with $k = {", join(', ', factor($k)), "}";
  84. $last_value = $k + 1;
  85. return $k;
  86. }
  87. }
  88. }
  89. sub a($n) {
  90. for (my $p = 5 ; ; $p = next_prime($p)) {
  91. my $ip = prime_count($p);
  92. if ($ip > $n) {
  93. my $x = scompo($p);
  94. my $fmax = vecmax(factor($x));
  95. my $ifmax = prime_count($fmax);
  96. if ($ip - $ifmax == $n) {
  97. my $y = $fmax * snumbr($p - $fmax);
  98. if ($x == $y) {
  99. #say "Found: p = $p";
  100. return $p;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. say a(1);
  107. say a(2);
  108. say a(3);
  109. say a(4);
  110. #say a(5);
  111. #say a(6);
  112. #a(4)
  113. #say a(7);
  114. __END__
  115. #~ a(n) = {
  116. #~ if(n <= 5, return(n));
  117. #~ my(p = precprime(n),
  118. #~ res = p * (n - p));
  119. #~ if(p == n, return(p), p = precprime(n - 2); res = p * a(n - p); while(res > (n - p) * p && p > 2, p = precprime(p - 1); res = min(res, a(n - p) * p)); res)}
  120. sub a($n, $cache={}) {
  121. if ($n <= 5) {
  122. return $n;
  123. }
  124. if (exists $cache->{$n}) {
  125. #say "cache hit";
  126. return $cache->{$n};
  127. }
  128. my $p = prev_prime($n+1);
  129. my $res = $p*($n-$p);
  130. if ($p == $n) {
  131. return $p;
  132. }
  133. $p = prev_prime($n-1);
  134. $res = $p*a($n-$p, $cache);
  135. while ($res > ($n-$p)*$p and $p > 2) {
  136. $p = prev_prime($p);
  137. $res = vecmin($res, a($n-$p, $cache)*$p);
  138. }
  139. return ($cache->{$n} = $res);
  140. }
  141. #say a(int rand 10**9);
  142. #__END__
  143. foreach my $n(1..100) {
  144. #say "a($n) = ", a($n);
  145. if (a($n) == $n) {
  146. say $n;
  147. }
  148. }
  149. __END__
  150. isok(k, n) = my(f=factor(k)); sum(j=1, #f~, f[j, 1]*f[j, 2]) == n;
  151. snumbr(n) = my(k=2); while(!isok(k, n), k++); k; /* A056240 */
  152. scompo(n) = forcomposite(k=4, , if (isok(k, n), return(k))); /* A288814 */
  153. a(n) = {forprime(p=5, , ip = primepi(p); if (ip > n, x = scompo(p); fmax = vecmax(factor(x)[, 1]); ifmax = primepi(fmax); if (ip - ifmax == n, y = fmax*snumbr(p - fmax; ); if (y == x, return (p); ); ); ); ); }
  154. __END__
  155. a(n) = {
  156. if(n <= 5, return(n));
  157. my(p = precprime(n), res = p * (n - p)); if(p == n, return(p), p = precprime(n - 2); res = p * a(n - p); while(res > (n - p) * p && p > 2, p = precprime(p - 1); res = min(res, a(n - p) * p)); res)
  158. }
  159. __END__
  160. use 5.014;
  161. use ntheory qw(:all vecsum);
  162. my $n = 2;
  163. forprimes {
  164. if (vecsum(factor($_)) == $n) {
  165. say "a($n) = $_";
  166. ++$n;
  167. }
  168. } 5, 1e12;
  169. __END__
  170. var P = primes(1e6.prime)
  171. var sum = 0
  172. for n in (^1e6) {
  173. sum += P[n]
  174. }
  175. say sum
  176. Cf. A000203, A064987
  177. __END__
  178. use 5.014;
  179. use ntheory qw(forprimes is_prime);
  180. forprimes {
  181. if ((2*$_ + 1)%5==0 and is_prime((2*$_ + 1)/5)) {
  182. say $_;
  183. }
  184. } 6e6;
  185. __END__
  186. #say bern(502)
  187. for n in (500 `downto` 500-100) {
  188. say bern(n)
  189. }
  190. __END__
  191. #~ func f(n) {
  192. #~ }
  193. #~ func foo(n, k) {
  194. #~ var sum = 0
  195. #~ #var prod = 1
  196. #~ for i in (0..k) {
  197. #~ var prod = 1
  198. #~ prod *= binomial(k+1, i)
  199. #~ #prod *= n**(k + 1 - i)
  200. #~ prod *= euler(i)
  201. #~ sum += prod
  202. #~ }
  203. #~ #prod
  204. #~ sum
  205. #~ }
  206. #var A028296 = [1, -1, 5, -61, 1385, -50521, 2702765, -199360981, 19391512145, -2404879675441, 370371188237525, -69348874393137901, 15514534163557086905, -4087072509293123892361, 1252259641403629865468285, -441543893249023104553682821, 177519391579539289436664789665]
  207. func a(n) {
  208. sum(0 .. floor((n-1)/2), {|k|
  209. euler(2*k)
  210. #A028296[k]
  211. })
  212. }
  213. for n in (0..500) {
  214. say (n, ' ', a(n))
  215. }
  216. __END__
  217. var sum = 0.float
  218. var x = -1/4
  219. say log(1 + tanh(x))*exp(x)
  220. for n in (0..400) {
  221. sum += (x**n * a(n) / n!)
  222. #say sum
  223. }
  224. say sum
  225. #for n in (0..10) {
  226. #say 20.of { foo(n, _) }
  227. #}
  228. #say a(100)
  229. #say 100.of(a)
  230. #say 10.of(a)
  231. __END__
  232. #say a(1000)
  233. for n in (0..100) {
  234. say (n, ' ', a(n))
  235. }
  236. #~ say 20.of(a)
  237. #~ say 20.of { (euler(_, 1/2) + euler(_, 1)) * 2**_ }
  238. #~ a(n) = Sum_{k=0..n-1} binomial(n, k) * euler(k). - ~~~~