这是不清楚你在问什么,但我会尝试猜测。
我们首先为实数z
解出方程z^z = a
。令u
和v
分别为z
向上舍入。在三个候选人中,(u,u)
,(v,u)
,(u,v)
我们选择最大的那个不超过a
。
示例:出具者案例a = 2000
。我们通过数值方法(见下文)解决z^z = 2000
以获得近似解决方案z = 4.8278228255818725
。我们向下取整以获得u = 4
和v = 5
。我们现在有三个候选人,4^4 = 256
,4^5 = 1023
和5^4 = 625
。他们都小于2000
,所以我们采取给出最大答案的是x = 4
,y = 5
。
这里是Python代码。功能solve_approx
做你想做的。它适用于a >= 3
。我相信你自己可以应付案件a = 1
和a = 2
。
import math
def solve(a):
""""Solve the equation x^x = a using Newton's method"""
x = math.log(a)/math.log(math.log(a)) # Initial estimate
while abs (x ** x - a) > 0.1:
x = x - (x ** x - a)/(x ** x * (1 + math.log(x)))
return x
def solve_approx(a):
""""Find two integer numbers x and y such that x^y is smaller than
a but as close to it as possible, and try to make x and y as equal
as possible."""
# First we solve exactly to find z such that z^z = a
z = solve(a)
# We round z up and down
u = math.floor(z)
v = math.ceil(z)
# We now have three possible candidates to choose from:
# u ** zdwon, v ** u, u ** v
candidates = [(u, u), (v, u), (u, v)]
# We filter out those that are too big:
candidates = [(x,y) for (x,y) in candidates if x ** y <= a]
# And we select the one that gives the largest result
candidates.sort(key=(lambda key: key[0] ** key[1]))
return candidates[-1]
这里是一个小演示:
>>> solve_approx(5)
solve_approx(5)
(2, 2)
>>> solve_approx(100)
solve_approx(100)
(3, 4)
>>> solve_approx(200)
solve_approx(200)
(3, 4)
>>> solve_approx(1000)
solve_approx(1000)
(5, 4)
>>> solve_approx(1000000)
solve_approx(1000000)
(7, 7)
请添加更多的语句。很显然,如果y = 1,您总是有选项x = A和y = 1。 –
另外,“最小的x和y”是什么意思?这不是一个数学表述。 – Jon
x和y是整数,还是可以浮动? –