smallest_number_with_at_least_n_divisors.sf 782 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 15 May 2021
  4. # https://github.com/trizen
  5. # Generate the smallest number that has at least n divisors.
  6. # See also:
  7. # https://oeis.org/A061799 -- Smallest number with at least n divisors.
  8. func smallest_number_with_at_least_n_divisors(threshold, least_solution = Inf, k = 1, max_a = Inf, solutions = 1, n = 1) {
  9. if (solutions >= threshold) {
  10. return n
  11. }
  12. var p = k.prime
  13. for a in (1 .. max_a) {
  14. n *= p
  15. break if (n > least_solution)
  16. least_solution = __FUNC__(threshold, least_solution, k+1, a, solutions * (a + 1), n)
  17. }
  18. return least_solution
  19. }
  20. say smallest_number_with_at_least_n_divisors(60) #=> 5040
  21. say smallest_number_with_at_least_n_divisors(1000) #=> 245044800