sigma_p_adic.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 14 November 2016
  5. # Website: https://github.com/trizen
  6. # An interesting function that computes the sum of
  7. # divisors (excluding the trivial divisors 1 and n),
  8. # each divisor raised to its p-adic valuation ν_d(n!).
  9. # For prime numbers, the value of `sigma_p_adic(p)` is 0.
  10. # See also:
  11. # https://en.wikipedia.org/wiki/P-adic_order
  12. # https://en.wikipedia.org/wiki/Legendre%27s_formula
  13. use 5.020;
  14. use strict;
  15. use warnings;
  16. use experimental qw(signatures);
  17. use ntheory qw(divisors forcomposites todigits vecsum);
  18. sub factorial_power ($n, $p) {
  19. ($n - vecsum(todigits($n, $p))) / ($p - 1);
  20. }
  21. sub sigma_p_adic ($n) {
  22. my @d = divisors($n);
  23. shift @d; # remove the first divisor (which is: 1)
  24. pop @d; # remove the last divisor (which is: n)
  25. my $s = 0;
  26. foreach my $d (@d) {
  27. $s += $d**factorial_power($n, $d);
  28. }
  29. return $s;
  30. }
  31. forcomposites {
  32. say $_, "\t", sigma_p_adic($_);
  33. } 30;
  34. __END__
  35. 4 8
  36. 6 25
  37. 8 144
  38. 9 81
  39. 10 281
  40. 12 1367
  41. 14 2097
  42. 15 854
  43. 16 33856
  44. 18 72394
  45. 20 266965
  46. 21 20026
  47. 22 524409
  48. 24 4271689
  49. 25 15625
  50. 26 8388777
  51. 27 1595052
  52. 28 33622565
  53. 30 71978959