bi-unitary_divisors.sf 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/ruby
  2. # Generate the bi-unitary divisors of n.
  3. # See also:
  4. # https://oeis.org/A188999
  5. # https://oeis.org/A222266
  6. func gcud(n, m) { # greatest common unitary divisor of n and m
  7. # Equivalent with:
  8. # return (udivisors(n) & udivisors(m) -> max)
  9. var g = gcd(n, m)
  10. while ((var t = gcd(g, n/g)) != 1) {
  11. g /= t
  12. }
  13. while ((var t = gcd(g, m/g)) != 1) {
  14. g /= t
  15. }
  16. return g
  17. }
  18. func bi_unitary_divisors(n) is cached {
  19. var d = [1]
  20. for p,e in (n.factor_exp) {
  21. var r = 1
  22. d << gather {
  23. e.times {
  24. r *= p
  25. take(d.map {|u| u*r }...) if (gcud(r, n/r) == 1)
  26. }
  27. }...
  28. }
  29. return d.sort
  30. }
  31. for n in (1..20) {
  32. say "bi-divisors of #{n} = #{bi_unitary_divisors(n)}"
  33. assert_eq(bi_unitary_divisors(n).len, n.bsigma0)
  34. assert_eq(bi_unitary_divisors(n).sum, n.bsigma)
  35. }
  36. __END__
  37. bi-divisors of 1 = [1]
  38. bi-divisors of 2 = [1, 2]
  39. bi-divisors of 3 = [1, 3]
  40. bi-divisors of 4 = [1, 4]
  41. bi-divisors of 5 = [1, 5]
  42. bi-divisors of 6 = [1, 2, 3, 6]
  43. bi-divisors of 7 = [1, 7]
  44. bi-divisors of 8 = [1, 2, 4, 8]
  45. bi-divisors of 9 = [1, 9]
  46. bi-divisors of 10 = [1, 2, 5, 10]
  47. bi-divisors of 11 = [1, 11]
  48. bi-divisors of 12 = [1, 3, 4, 12]
  49. bi-divisors of 13 = [1, 13]
  50. bi-divisors of 14 = [1, 2, 7, 14]
  51. bi-divisors of 15 = [1, 3, 5, 15]
  52. bi-divisors of 16 = [1, 2, 8, 16]
  53. bi-divisors of 17 = [1, 17]
  54. bi-divisors of 18 = [1, 2, 9, 18]
  55. bi-divisors of 19 = [1, 19]
  56. bi-divisors of 20 = [1, 4, 5, 20]