81

What is the fastest way to check if $x^y > y^x$ if I were writing a computer program to do that?

The issue is that $x$ and $y$ can be very large.

Cameron Buie
  • 98,939
  • 9
  • 90
  • 207
learner
  • 857
  • 2
  • 8
  • 10

3 Answers3

105

If both $x$ and $y$ are positive then you can just check: $$ \frac{\log(x)}{x} \gt \frac{\log(y)}{y}$$ so if both $x$ and $y$ are greater than $e \approx 2.7183$ then you can just check: $$x \lt y$$

Henry
  • 142,043
  • 9
  • 114
  • 228
  • But how `x < y`? How can we omit the `log`s? Intuitively it seems right, but is it really so? – learner Oct 07 '13 at 11:18
  • 27
    @learner, the point is that the function $log(x)/x$ has a single maximum at $x=e$ and is decreasing after that. – lhf Oct 07 '13 at 11:19
  • 1
    I don't know why this answer is the accepted one, but multiplication is usually faster than division, so I assume $y log(x) > x log(y)$ is faster than this one. – Christian Fries Oct 07 '13 at 12:15
  • 22
    @ChristianFries, this answer is the accepted one because of the massive optimization given in the second half of the answer. Even if you don't use the monotonicity of $\frac{\log{x}}{x}$, the fact that it's a single function can make repeated queries much faster. – jwg Oct 07 '13 at 12:29
  • so can you do similar optimisations for if x and y are both less than e? – Chris Oct 07 '13 at 17:01
  • 5
    If $x$ and $y$ are both between $0$ and $e$ then the test becomes $x \gt y$. The hard part is when one is below and one above: for example $2^4 = 4^2$ and $\frac{\log 2}{2} = \frac{\log 4}{4}$ – Henry Oct 07 '13 at 17:16
  • @Henry -- Did you mean "... **1** and $e$"? When $log$'s operand is less than one, it becomes negative and one has to be careful with inequalities with one side being negative and other being positive. – Happy Green Kid Naps Oct 08 '13 at 03:28
  • @HappyGreenKidNaps: Using $\frac{\log x}{x}$ rather than $\frac{x}{\log x}$ means there is no problem. In fact it gets easier: if **either** $x$ or $y$ are less than or equal to $1$ (and both positive) then you can use $x \gt y$ as the test. – Henry Oct 08 '13 at 07:38
  • Which means one has more permutations to test (I agree that it would still be *far* more efficient than calculating $x ^ y$ or $y ^ x$. – Happy Green Kid Naps Oct 08 '13 at 16:59
  • 1
    @Henry: Sorry for hijacking this comment thread (I will delete this comment in a moment), but I have a question to Henry: I noticed that you just removed your answer to my question on integer division. Was the answer wrong? – Sid Dec 20 '16 at 12:31
  • Also see [1](https://math.stackexchange.com/q/2276227/318073), [2](https://math.stackexchange.com/q/2546027/318073), [3](https://math.stackexchange.com/q/2425947/318073), [4](https://math.stackexchange.com/q/2414679/318073), [5](https://math.stackexchange.com/q/1788290/318073), [6](https://math.stackexchange.com/q/978078/318073), [7](https://math.stackexchange.com/q/1799159/318073), ... – Vepir Mar 18 '21 at 11:53
25

You might get by testing whether $y \log x > x \log y$, especially if the numbers are only moderately large.

lhf
  • 208,399
  • 15
  • 224
  • 525
6

We reduce the question to: is the quotient $\frac{x^y} {y^x}$ less or greater than $1$.

Noticing that $x > 0, y > 0,$ we take the xy-th root of the quotient:

Is the ratio $(x^y)^{\frac1{xy}} / (y^x)^{\frac1{xy}}$ less or greater than $1^{\frac1{xy}}$.

We reduce the complicated powers, the power of base 1 is 1:

is the ratio $x^{\frac1x} / y^{\frac1y}$ less or greater than 1.

is the function $f(x) = x^{\frac1x}$ increasing or decreasing at the interval $[x,y]$?

· If already known, via the derivate, that the function $f(x) = x^{\frac1x}$ has only one maximum, so an absolute maximum for $x = e$ , the point $(e, e^{\frac1e})$,

Consider that a) for $x > e$ and $y > e$ , the function $f$ is decreasing, which means

• if $e < x < y => f(x) > f(y)$. [both x and y are greater than e, the greater exponent wins.]

and b) for $x < e$ and $y < e$ the function $f$ is increasing, which means

• if $0 < x < y < e => f(x) < f(y)$. [both x and y are less than e, the greater base wins.]

• But if $x < e < y$, the comparison is not decided through the function’s behaviour. [e is between both.] We have to calculate both powers to compare the outcomes.

So there are pairs of $x,y$ existing for which $x^y = y^x$, with $x < e < y$.

For example, $x = √(√5)$ and y = √(√(5^5)) = √(√3125)
both x^y and y^x come out on ≈ 20,25372598…

Many pairs are findable, e.g. by setting y = a·x and resolving the equation (x^y) = (y^x), or

f(x) = f(y).

Milan
  • 1,583
  • 1
  • 10
  • 22
kees canal fp
  • 61
  • 1
  • 1
  • Welcome to MSE. For some basic information about writing mathematics at this site see, *e.g.*, [basic help on mathjax notation](/help/notation), [mathjax tutorial and quick reference](//math.meta.stackexchange.com/q/5020), [main meta site math tutorial](//meta.stackexchange.com/a/70559) and [equation editing how-to](//math.meta.stackexchange.com/q/1773). – José Carlos Santos May 23 '19 at 06:33