12345678910111213141516171819202122232425262728293031323334 |
- #!/usr/bin/ruby
- # Daniel "Trizen" Șuteu
- # Date: 15 May 2021
- # https://github.com/trizen
- # Diophantine reciprocals II
- # https://projecteuler.net/problem=110
- # See also:
- # https://oeis.org/A018892
- # Runtime: 1.281s
- func solve(threshold, least_solution = Inf, k = 1, max_a = Inf, solutions = 1, n = 1) {
- if (solutions > threshold) {
- return n
- }
- var p = k.prime
- for a in (1 .. max_a) {
- n *= p
- break if (n > least_solution)
- least_solution = __FUNC__(threshold, least_solution, k+1, a, solutions * (2*a + 1), n)
- }
- return least_solution
- }
- var LIMIT = 4e6
- say solve(2*LIMIT + 1)
|