4

If $a$ is given how can I calculate $x^y$ and $y^x$ the fastest way? Is there any other way than brute forcing? How is this type of equation called?

Let's say $x$ and $y$ must be $>1$ and non negative integers.

arminb
  • 317

1 Answers1

2

Let's analyze this a bit, assuming that $x \le y$:

  • For $x = 2$, you need to test $y = 2, 3, \dots, \lfloor \log_2 a \rfloor$.
  • For $x = 3$, you need to test $y = 3, 4, \dots, \lfloor \log_3 a \rfloor$.

    ...

  • The last $x$ is the biggest one for which $x^x \le a$. For example, for $a = 2^{64}$ (the first nonnegative integer number that fits in 64 bits), that's $x = 16$.

So, let $a = 2^{64}-1$. Then, we have $246$ candidates for a solution:

\begin{align*} &x = 2 \quad \Rightarrow \quad y \in \{2,3,\dots,63\}, \\ &x = 3 \quad \Rightarrow \quad y \in \{3,4,\dots,40\}, \\ &x = 4 \quad \Rightarrow \quad y \in \{4,5,\dots,31\}, \\ &x = 5 \quad \Rightarrow \quad y \in \{5,6,\dots,27\}, \\ &x = 6 \quad \Rightarrow \quad y \in \{6,7,\dots,24\}, \\ &x = 7 \quad \Rightarrow \quad y \in \{7,8,\dots,22\}, \\ &x = 8 \quad \Rightarrow \quad y \in \{8,9,\dots,21\}, \\ &x = 9 \quad \Rightarrow \quad y \in \{9,10,\dots,20\}, \\ &x = 10 \quad \Rightarrow \quad y \in \{10,11,\dots,19\}, \\ &x = 11 \quad \Rightarrow \quad y \in \{11,12,\dots,18\}, \\ &x = 12 \quad \Rightarrow \quad y \in \{12,13,\dots,17\}, \\ &x = 13 \quad \Rightarrow \quad y \in \{13,14,\dots,17\}, \\ &x = 14 \quad \Rightarrow \quad y \in \{14,15,16\}, \\ &x = 15 \quad \Rightarrow \quad y \in \{16\}, \end{align*}

I draw two conclusions from here:

  1. Bruteforcing is simple.

  2. Only a negligible number of these systems ($246$ among the first $2^{64}-1$) have solutions.

It's easier to go through all possible $(x,y)$, construct all $246$ values of $(x,y,a)$, put them in a file or a database, and just search for a solution when one is needed (and, most likely, doesn't exist).

The larger your numbers are, the less likely it is that you'll have a solution for any given $a$, so there is no point in going beyond $64$ bit (actually, there is no point going even that far).

Vedran Šego
  • 11,372
  • Very good, very understandable. Thank you! Hvala Vedrane! ;) – arminb Aug 07 '13 at 21:38
  • @arminb I'm glad I could help. Drago mi je da sam mogao pomoći. :-) – Vedran Šego Aug 07 '13 at 22:15
  • I have a follow up question. How to find out $x$ for $x^x \le a$ the fastest way? – arminb Aug 07 '13 at 22:29
  • Given the speed of growth of such function, a simple while loop in any language will be very quick. For example, Mathematica on my computer (3+ years old) finds a solution for $$a = 2^{100000} - 1 = 2^{10^5} - 1$$ in about 3 seconds (it's $x \le 7740$). Notice that this number needs $10^5$ bits, i.e., around 12.5KiB of memory, so the arithmetic operations are quite slow (they need to be software-simulated, as the processor doesn't support that). – Vedran Šego Aug 07 '13 at 22:42
  • At the moment, I have no idea how to do it without the aid of computer, apart from some numerical methods (bisection and likes), which would be too slow to do by hand (just try getting $7740^{7740}$ by hand ;-)). – Vedran Šego Aug 07 '13 at 22:43
  • Yes, trying each $x^x$ in a while loop is one possibility. But I hoped there was a more elegant way to obtain $x$. – arminb Aug 07 '13 at 22:47
  • 1
    That's a bit subjective. For me, simple while is elegant. If you prefer Lambert W function, check here. – Vedran Šego Aug 07 '13 at 23:01