brazilian_primes_constant.pl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/perl
  2. # Compute the decimal expansion of the sum of reciprocals of Brazilian primes, also called the Brazilian primes constant.
  3. # The constant begins as:
  4. # 0.3317544666
  5. # OEIS sequences:
  6. # https://oeis.org/A085104 (Brazillian primes)
  7. # https://oeis.org/A306759 (Decimal expansion of the sum of reciprocals of Brazilian primes)
  8. use 5.020;
  9. use warnings;
  10. use experimental qw(signatures);
  11. use ntheory qw(:all);
  12. use Math::AnyNum;
  13. sub brazillian_constant ($lim) {
  14. my $N = Math::GMPz->new("$lim");
  15. my $q = Math::GMPq->new(0);
  16. my $z = Math::GMPz->new(0);
  17. my $sum = Math::MPFR::Rmpfr_init2(192);
  18. Math::MPFR::Rmpfr_set_ui($sum, 0, 0);
  19. my %seen;
  20. # The algorithm for generating the Brazillian primes is due to M. F. Hasler.
  21. # See: https://oeis.org/A085104
  22. forprimes {
  23. my $K = $_;
  24. for my $n (2 .. rootint($N - 1, $K - 1)) {
  25. Math::GMPz::Rmpz_ui_pow_ui($z, $n, $K);
  26. Math::GMPz::Rmpz_sub_ui($z, $z, 1);
  27. Math::GMPz::Rmpz_divexact_ui($z, $z, $n - 1);
  28. if (
  29. is_prob_prime(
  30. Math::GMPz::Rmpz_fits_ulong_p($z)
  31. ? Math::GMPz::Rmpz_get_ui($z)
  32. : Math::GMPz::Rmpz_get_str($z, 10)
  33. )
  34. ) {
  35. # Conjecture: duplicate terms may happen only for t = 2^k-1, for some k
  36. if ((($z + 1) & $z) == 0) {
  37. next if $seen{$z}++;
  38. }
  39. if ($z < $N) {
  40. Math::GMPq::Rmpq_set_ui($q, 1, 1);
  41. Math::GMPq::Rmpq_set_den($q, $z);
  42. Math::MPFR::Rmpfr_add_q($sum, $sum, $q, 0);
  43. }
  44. }
  45. }
  46. } 3, logint($N + 1, 2);
  47. return Math::AnyNum->new($sum);
  48. }
  49. foreach my $n (1 .. 14) {
  50. say "B(10^$n) ~ ", brazillian_constant(Math::GMPz->new(10)**$n)->round(-32);
  51. }
  52. __END__
  53. B(10^1) ~ 0.14285714285714285714285714285714
  54. B(10^2) ~ 0.28899272838682348594073100542184
  55. B(10^3) ~ 0.32290223556269144810843769843366
  56. B(10^4) ~ 0.32952368063536693571523726793301
  57. B(10^5) ~ 0.33121713119461798438057432911381
  58. B(10^6) ~ 0.33160386963492172892306297309503
  59. B(10^7) ~ 0.33171391586547473334091623260371
  60. B(10^8) ~ 0.33174341910781704122196304798802
  61. B(10^9) ~ 0.33175132673949885380067237840723
  62. B(10^10) ~ 0.33175356516689372562521462231951
  63. B(10^11) ~ 0.33175420579318423292974799113059
  64. B(10^12) ~ 0.33175439067722742680152185017303
  65. B(10^13) ~ 0.33175444440331880514669754839817
  66. B(10^14) ~ 0.33175446011369675270545267094599
  67. B(10^15) ~ 0.33175446473544852087966767749508
  68. B(10^16) ~ 0.33175446610148680800864203095541 -- took 1 minute
  69. B(10^17) ~ 0.33175446650734519516960634448563 -- took 4 minutes
  70. B(10^18) ~ 0.33175446662828756863723305575693 -- took 20 minutes
  71. B(10^19) ~ 0.33175446666446018177571079766533 -- took 39 minutes
  72. B(10^20) ~ 0.33175446667530957668020208565143 -- took 5 hours and 23 minutes