k-powerful_numbers.sf 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 11 February 2020
  4. # https://github.com/trizen
  5. # Fast recursive algorithm for generating all the k-powerful numbers <= n.
  6. # A positive integer n is considered k-powerful, if for every prime p that divides n, so does p^k.
  7. # Example:
  8. # 2-powerful = a^2 * b^3, for a,b >= 1
  9. # 3-powerful = a^3 * b^4 * c^5, for a,b,c >= 1
  10. # 4-powerful = a^4 * b^5 * c^6 * d^7, for a,b,c,d >= 1
  11. # OEIS:
  12. # https://oeis.org/A001694 -- 2-powerful numbers
  13. # https://oeis.org/A036966 -- 3-powerful numbers
  14. # https://oeis.org/A036967 -- 4-powerful numbers
  15. # https://oeis.org/A069492 -- 5-powerful numbers
  16. # https://oeis.org/A069493 -- 6-powerful numbers
  17. func k_powerful_numbers(n, k=2) {
  18. var powerful = []
  19. func (m,r) {
  20. if (r < k) {
  21. powerful << m
  22. return nil
  23. }
  24. for a in (1 .. iroot(idiv(n,m), r)) {
  25. if (r > k) {
  26. a.is_coprime(m) || next
  27. a.is_squarefree || next
  28. }
  29. __FUNC__(m * a**r, r-1)
  30. }
  31. }(1, 2*k - 1)
  32. powerful.sort
  33. }
  34. for k in (1..10) {
  35. say ("#{'%2d' % k}-powerful: ", k_powerful_numbers(5**k, k).join(', '))
  36. }
  37. __END__
  38. 1-powerful: 1, 2, 3, 4, 5
  39. 2-powerful: 1, 4, 8, 9, 16, 25
  40. 3-powerful: 1, 8, 16, 27, 32, 64, 81, 125
  41. 4-powerful: 1, 16, 32, 64, 81, 128, 243, 256, 512, 625
  42. 5-powerful: 1, 32, 64, 128, 243, 256, 512, 729, 1024, 2048, 2187, 3125
  43. 6-powerful: 1, 64, 128, 256, 512, 729, 1024, 2048, 2187, 4096, 6561, 8192, 15625
  44. 7-powerful: 1, 128, 256, 512, 1024, 2048, 2187, 4096, 6561, 8192, 16384, 19683, 32768, 59049, 65536, 78125
  45. 8-powerful: 1, 256, 512, 1024, 2048, 4096, 6561, 8192, 16384, 19683, 32768, 59049, 65536, 131072, 177147, 262144, 390625
  46. 9-powerful: 1, 512, 1024, 2048, 4096, 8192, 16384, 19683, 32768, 59049, 65536, 131072, 177147, 262144, 524288, 531441, 1048576, 1594323, 1953125
  47. 10-powerful: 1, 1024, 2048, 4096, 8192, 16384, 32768, 59049, 65536, 131072, 177147, 262144, 524288, 531441, 1048576, 1594323, 2097152, 4194304, 4782969, 8388608, 9765625