110 Diophantine reciprocals II.sf 639 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 15 May 2021
  4. # https://github.com/trizen
  5. # Diophantine reciprocals II
  6. # https://projecteuler.net/problem=110
  7. # See also:
  8. # https://oeis.org/A018892
  9. # Runtime: 1.281s
  10. func solve(threshold, least_solution = Inf, k = 1, max_a = Inf, solutions = 1, n = 1) {
  11. if (solutions > threshold) {
  12. return n
  13. }
  14. var p = k.prime
  15. for a in (1 .. max_a) {
  16. n *= p
  17. break if (n > least_solution)
  18. least_solution = __FUNC__(threshold, least_solution, k+1, a, solutions * (2*a + 1), n)
  19. }
  20. return least_solution
  21. }
  22. var LIMIT = 4e6
  23. say solve(2*LIMIT + 1)