logs.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/perl
  2. # Least k > 0 such that sigma(k!) >= n*k!, with a(0) = a(1) = 1.
  3. # https://oeis.org/A061556
  4. # Known terms:
  5. # 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, 7879, 13859, 24247, 42683, 75037
  6. use 5.020;
  7. use strict;
  8. use warnings;
  9. use experimental qw(signatures);
  10. use ntheory qw(forprimes vecsum todigits :all);
  11. use Math::AnyNum qw(ipow factorial lgamma log );
  12. sub factorial_power ($n, $p) {
  13. ($n - vecsum(todigits($n, $p))) / ($p - 1);
  14. }
  15. my $t = Math::GMPz::Rmpz_init();
  16. sub sigma_of_factorial ($n) {
  17. #my $sigma = Math::GMPz::Rmpz_init_set_ui(1);
  18. my $sigma = 0;
  19. forprimes {
  20. my $p = $_;
  21. my $k = factorial_power($n, $p);
  22. # (p^(k+1) - 1) / (p-1)
  23. $sigma += (log($p) * ($k + 1)) - log($p - 1);
  24. }
  25. $n;
  26. return $sigma;
  27. }
  28. say sigma_of_factorial(42683);
  29. say sigma_of_factorial(75037);
  30. say sigma_of_factorial(126000);
  31. # Least k > 0 such that sigma(k!) >= n*k!.
  32. my @array = (1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, 7879, 13859, 24247, 42683, 75037, 131707);
  33. #my @array = (1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, 7879, 13859, 24247, 42683, 75037, 131707, 230773, 405401, 710569, 1246379, 2185021, 3831913, 6720059, 11781551, 20657677);
  34. #my @array = (0..12, 1451, 2549, 4483, 7879, 13859, 24247, 42683, 75037, 131707, 230773, 405401, 710569, 1246379, 2185021, 3831913, 6720059, 11781551, 20657677, 36221753, 63503639, 111333529, 195199289, 342243479, 600036989);
  35. say "Sanity checking...";
  36. foreach my $n (0 .. $#array) {
  37. my $k = $array[$n];
  38. say "[$n] Testing: $k";
  39. next if $k < 1000;
  40. next if $k == 1;
  41. my $s = Math::AnyNum->new(sigma_of_factorial($k));
  42. my $t = log($n) + lgamma($k + 1);
  43. $s >= $t or die "error for $k";
  44. next if $k < 1000;
  45. my $s2 = sigma_of_factorial($k - 1);
  46. my $t2 = log($n) + lgamma($k);
  47. $s2 >= $t2 and die "too large: $k -- ($s2 >= $t2)";
  48. }
  49. say "Test passed...";
  50. my $n = 21;
  51. local $| = '1';
  52. my $k = 131707 - 100;
  53. my $factorial = lgamma($k + 1);
  54. #$factorial = Math::GMPz->new("$factorial");
  55. for (; $k <= 1e6 ; ++$k) {
  56. say "Testing: $k" if ($k % 1000 == 0);
  57. while (sigma_of_factorial($k) >= log($n) + $factorial) {
  58. print($k, ", ");
  59. if ($k > 131707) {
  60. die "Found: $k";
  61. }
  62. ++$n;
  63. }
  64. #$factorial *= ($k + 1);
  65. #Math::GMPz::Rmpz_mul_ui($factorial, $factorial, $k+1);
  66. $factorial += log($k + 1);
  67. }
  68. __END__
  69. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, perl r.pl 2.13s user 0.01s system 99% cpu 2.140 total
  70. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, perl r.pl 0.77s user 0.02s system 99% cpu 0.794 total
  71. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, perl r.pl 0.77s user 0.02s system 99% cpu 0.788 total
  72. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, perl r.pl 0.40s user 0.02s system 99% cpu 0.423 total
  73. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, perl r.pl 0.19s user 0.01s system 99% cpu 0.203 total
  74. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, perl r.pl 2.94s user 0.01s system 99% cpu 2.967 total
  75. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, perl r.pl 2.95s user 0.02s system 99% cpu 2.974 total
  76. 1, 1, 3, 5, 9, 14, 23, 43, 79, 149, 263, 461, 823, 1451, 2549, 4483, perl r.pl 2.93s user 0.02s system 98% cpu 3.003 total