primorial_deflation.sf 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 15 April 2019
  4. # https://github.com/trizen
  5. # Represent a given number as a product of primorials (if possible).
  6. # The sequence of numbers that can be represented as a product of primorials, is given by:
  7. # https://oeis.org/A025487
  8. # Among other terms, the sequence includes the factorials and the highly composite numbers.
  9. # See also:
  10. # https://oeis.org/A181815 -- "primorial deflation" of A025487(n)
  11. # https://oeis.org/A108951 -- "primorial inflation" of n
  12. func primorial_deflation (n) {
  13. var prod = 1
  14. while (n > 1) {
  15. var g = n.gpf # greatest prime factor
  16. var p = g.primorial
  17. n /= p
  18. n.is_int || return nil
  19. prod *= g
  20. }
  21. return prod
  22. }
  23. func primorial_deflation_bag(n) {
  24. var prod = 1
  25. var f = Bag(n.factor...)
  26. while (f) {
  27. var p = f.max
  28. f -= p.primes
  29. prod *= p
  30. }
  31. return prod
  32. }
  33. func primorial_deflation_fast (n) {
  34. n.factor_prod {|p,e|
  35. (p / (p > 2 ? p.prev_prime : 1))**e
  36. }
  37. }
  38. var arr = 15.of { primorial_deflation(_!) } # https://oeis.org/A307035
  39. say arr #=> [1, 1, 2, 3, 12, 20, 60, 84, 672, 1512, 5040, 7920, 47520, 56160, 157248]
  40. say arr.map {|n| n.factor_prod {|p,e| p.primorial**e } } #=> [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200]
  41. assert_eq(15.of { primorial_deflation(_!) }, 15.of { primorial_deflation_bag(_!) })
  42. assert_eq(15.of { primorial_deflation(_!) }, 15.of { primorial_deflation_fast(_!) })
  43. var n = 14742217487368791965347653720647452690286549052234444179664342042930370966727413549068727214664401976854238590421417268673037399536054005777393104248210539172848500736334237168727231561710827753972114334247396552090671649834020135652920430241738510495400044737265204738821393451152066370913670083496651044937158497896720493198891148968218874744806522767468280764179516341996273430700779982929787918221844760577694188288275419541410142336911631623319041967633591283303769044016192030492715535641753600000
  44. say primorial_deflation(n) #=> 52900585920
  45. say primorial_deflation_bag(n) #=> 52900585920
  46. say primorial_deflation_fast(n) #=> 52900585920