這是不清楚你在問什麼,但我會嘗試猜測。
我們首先爲實數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是整數,還是可以浮動? –