inverse_of_usigma_function.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/perl
  2. # Given a positive integer `n`, this algorithm finds all the numbers k
  3. # such that usigma(k) = n, where `usigma(k)` is the sum of the unitary divisors of `k`.
  4. # usigma(n) is multiplicative with usigma(p^k) = p^k + 1.
  5. # See also:
  6. # https://oeis.org/A034448 -- usigma(n)
  7. # https://home.gwu.edu/~maxal/gpscripts/
  8. use utf8;
  9. use 5.020;
  10. use strict;
  11. use warnings;
  12. use ntheory qw(:all);
  13. use experimental qw(signatures);
  14. sub inverse_usigma ($n) {
  15. my %r = (1 => [1]);
  16. foreach my $d (divisors($n)) {
  17. my $D = subint($d, 1);
  18. is_prime_power($D) || next;
  19. my %temp;
  20. foreach my $f (divisors(divint($n, $d))) {
  21. if (exists $r{$f}) {
  22. push @{$temp{mulint($f, $d)}}, map { mulint($D, $_) }
  23. grep { gcd($D, $_) == 1 } @{$r{$f}};
  24. }
  25. }
  26. while (my ($key, $value) = each(%temp)) {
  27. push @{$r{$key}}, @$value;
  28. }
  29. }
  30. return if not exists $r{$n};
  31. return sort { $a <=> $b } @{$r{$n}};
  32. }
  33. my $n = 186960;
  34. say "Solutions for usigma(x) = $n: ", join(' ', inverse_usigma($n));
  35. __END__
  36. Solutions for usigma(x) = 186960: 90798 108558 109046 113886 116835 120620 123518 123554 130844 131868 136419 138651 145484 148004 153495 155795 163503 163583 165771 166463 173907 174899 176823 179147 182003 185579 186089 186959