unitary_powerfree_divisors.sf 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/ruby
  2. # A simple algorithm for generating the unitary k-powerfree divisors of a given number n.
  3. # See also:
  4. # https://oeis.org/A092261
  5. # https://mathworld.wolfram.com/Powerfree.html
  6. # https://en.wikipedia.org/wiki/Unitary_divisor
  7. func unitary_powerfree_divisors(n, k=2) {
  8. var d = [1]
  9. for p,e in (n.factor_exp) {
  10. e < k || next
  11. var r = p**e
  12. d << d.map {|t| t*r }...
  13. }
  14. return d.sort
  15. }
  16. for n in (1..20) {
  17. say "unitary squarefree divisors of #{n} = #{unitary_powerfree_divisors(n, 2)}"
  18. assert_eq(unitary_powerfree_divisors(n, 2), n.udivisors.grep { .is_powerfree(2) })
  19. assert_eq(unitary_powerfree_divisors(n, 3), n.udivisors.grep { .is_powerfree(3) })
  20. assert_eq(unitary_powerfree_divisors(n, 4), n.udivisors.grep { .is_powerfree(4) })
  21. }
  22. __END__
  23. unitary squarefree divisors of 1 = [1]
  24. unitary squarefree divisors of 2 = [1, 2]
  25. unitary squarefree divisors of 3 = [1, 3]
  26. unitary squarefree divisors of 4 = [1]
  27. unitary squarefree divisors of 5 = [1, 5]
  28. unitary squarefree divisors of 6 = [1, 2, 3, 6]
  29. unitary squarefree divisors of 7 = [1, 7]
  30. unitary squarefree divisors of 8 = [1]
  31. unitary squarefree divisors of 9 = [1]
  32. unitary squarefree divisors of 10 = [1, 2, 5, 10]
  33. unitary squarefree divisors of 11 = [1, 11]
  34. unitary squarefree divisors of 12 = [1, 3]
  35. unitary squarefree divisors of 13 = [1, 13]
  36. unitary squarefree divisors of 14 = [1, 2, 7, 14]
  37. unitary squarefree divisors of 15 = [1, 3, 5, 15]
  38. unitary squarefree divisors of 16 = [1]
  39. unitary squarefree divisors of 17 = [1, 17]
  40. unitary squarefree divisors of 18 = [1, 2]
  41. unitary squarefree divisors of 19 = [1, 19]
  42. unitary squarefree divisors of 20 = [1, 5]