highly_composite_numbers.sf 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/ruby
  2. # Translation of:
  3. # https://gist.github.com/dario2994/fb4713f252ca86c1254d
  4. # Generate all the highly composite numbers <= LIMIT.
  5. const LIMIT = (ARGV ? ARGV[0].to_i : 10**8)
  6. # Generates a list of the first primes (with product > LIMIT).
  7. func gen_primes() {
  8. var primes = []
  9. var primes_product = 1
  10. for (var p = 2; primes_product <= LIMIT; p.next_prime!) {
  11. primes << p
  12. primes_product *= p
  13. }
  14. return primes
  15. }
  16. # Generates a list of the hcn <= LIMIT.
  17. func gen_hcn() {
  18. var primes = gen_primes()
  19. var hcn = [[1, 1, []]]
  20. primes.each_kv {|i,p|
  21. var new_hcn = []
  22. hcn.each {|el|
  23. new_hcn << el
  24. next if (el[2].len < i)
  25. for e in (1 .. (p==2 ? LIMIT.ilog2 : el[2][i-1])) {
  26. var (n, sigma0, exps) = el...
  27. var new_exps = [exps...]
  28. n *= p**e
  29. sigma0 *= e+1
  30. new_exps << e
  31. break if (n > LIMIT)
  32. new_hcn << [n, sigma0, new_exps]
  33. }
  34. }
  35. hcn = [[1, 1, []]]
  36. new_hcn.sort.each {|el|
  37. if (el[1] > hcn[-1][1]) {
  38. hcn << el
  39. }
  40. }
  41. }
  42. return hcn.map{ .head }
  43. }
  44. say gen_hcn().join(', ')