inverse_of_sigma_function.sf 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/ruby
  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. func inverse_sigma(n, m=3) {
  8. return [1] if (n == 1)
  9. gather {
  10. for d in (n.divisors.grep {|d| d >= m }), p in (prime_divisors(d-1)) {
  11. var P = (d*(p-1) + 1)
  12. var k = (P.valuation(p) - 1)
  13. next if ((k < 1) || (P != p**(k+1)))
  14. take(__FUNC__(n/d, d).grep {|x|
  15. !(p `divides` x)
  16. }.scalar_mul(p**k)...)
  17. }
  18. }.uniq
  19. }
  20. for n in (1..70) {
  21. var arr = inverse_sigma(n) || next
  22. say "σ−¹(#{n}) = [#{arr.join(', ')}]";
  23. }
  24. __END__
  25. σ−¹(1) = [1]
  26. σ−¹(3) = [2]
  27. σ−¹(4) = [3]
  28. σ−¹(6) = [5]
  29. σ−¹(7) = [4]
  30. σ−¹(8) = [7]
  31. σ−¹(12) = [6, 11]
  32. σ−¹(13) = [9]
  33. σ−¹(14) = [13]
  34. σ−¹(15) = [8]
  35. σ−¹(18) = [10, 17]
  36. σ−¹(20) = [19]
  37. σ−¹(24) = [14, 15, 23]
  38. σ−¹(28) = [12]
  39. σ−¹(30) = [29]
  40. σ−¹(31) = [16, 25]
  41. σ−¹(32) = [21, 31]
  42. σ−¹(36) = [22]
  43. σ−¹(38) = [37]
  44. σ−¹(39) = [18]
  45. σ−¹(40) = [27]
  46. σ−¹(42) = [26, 20, 41]
  47. σ−¹(44) = [43]
  48. σ−¹(48) = [33, 35, 47]