inverse_of_sigma_function.pl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/perl
  2. # Given a positive integer `n`, this algorithm finds all the numbers k
  3. # such that sigma(k) = n, where `sigma(k)` is the sum of divisors of `k`.
  4. # Based on "invphi.gp" code by Max Alekseyev.
  5. # See also:
  6. # https://home.gwu.edu/~maxal/gpscripts/
  7. use utf8;
  8. use 5.020;
  9. use strict;
  10. use warnings;
  11. use ntheory qw(:all);
  12. use experimental qw(signatures);
  13. use List::Util qw(uniq);
  14. #use Math::AnyNum qw(:overload);
  15. binmode(STDOUT, ':utf8');
  16. sub inverse_sigma ($n, $m = 3) {
  17. return (1) if ($n == 1);
  18. my @R;
  19. foreach my $d (grep { $_ >= $m } divisors($n)) {
  20. foreach my $p (map { $_->[0] } factor_exp($d - 1)) {
  21. my $P = $d * ($p - 1) + 1;
  22. my $k = valuation($P, $p) - 1;
  23. next if (($k < 1) || ($P != $p**($k + 1)));
  24. push @R, map { $_ * $p**$k } grep { $_ % $p != 0; } __SUB__->($n/$d, $d);
  25. }
  26. }
  27. sort { $a <=> $b } uniq(@R);
  28. }
  29. foreach my $n (1 .. 70) {
  30. (my @inv = inverse_sigma($n)) || next;
  31. say "σ−¹($n) = [", join(', ', @inv), ']';
  32. }
  33. __END__
  34. σ−¹(1) = [1]
  35. σ−¹(3) = [2]
  36. σ−¹(4) = [3]
  37. σ−¹(6) = [5]
  38. σ−¹(7) = [4]
  39. σ−¹(8) = [7]
  40. σ−¹(12) = [6, 11]
  41. σ−¹(13) = [9]
  42. σ−¹(14) = [13]
  43. σ−¹(15) = [8]
  44. σ−¹(18) = [10, 17]
  45. σ−¹(20) = [19]
  46. σ−¹(24) = [14, 15, 23]
  47. σ−¹(28) = [12]
  48. σ−¹(30) = [29]
  49. σ−¹(31) = [16, 25]
  50. σ−¹(32) = [21, 31]
  51. σ−¹(36) = [22]
  52. σ−¹(38) = [37]
  53. σ−¹(39) = [18]
  54. σ−¹(40) = [27]
  55. σ−¹(42) = [26, 20, 41]
  56. σ−¹(44) = [43]
  57. σ−¹(48) = [33, 35, 47]