lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Jun 27, 2019 at 9:57 AM Philippe Verdy <verdy_p@wanadoo.fr> wrote:
but a basic asymptotic approcimation cannot work for any number x that sastisfies x^x > 15.5 (which can be very large).
So let's see where x^x does not return  positive infinite (starting at 2^1024).
We need the mathematic solution for x^x = 2^1024.

But let's start by how we can compute the base-2 exponent (which will be betwen 3 and 1024 for this problem) to get a rough estimate of the order of magnitude
log_2(x^x) =x*log_2(x) = 1024
so x= 1024/log_2(x). and then x must be below 

The problem states that x^x>15.5, so x >15.5/log_2(x) (this will be used for tuning the value of the mantissa and correct the effect of the approximate base-2 exponent.

There's not a lot of values of x that can qualify, it is in a small range in the open range (3.3, 395)
If x is to be restricted to be an integer, this x is in the integer range 4..394 (because 395^395>2^1024 and gives a positive infinite value for 64-bit IEEE doubles)

But in that case a table lookup is not possible (the integer range 4..394 would contain 390 double values in a static table)

So what is to approximate if the base-2 integer exponent of x^x and then we can correct the mantissa; and this requires amultiplication from the correct mantissa, by the approximation of the base-2 exponent.

This will still produce large errors compared to the recursive method using the derivative function of (x => x^x), which is (x => (ln(x) + 1) * x^x).

The recursive function will then loop by dividing the current approximation of x^x, by (log(x)+1), to form the new approximation by taking the average (it will have a fast convergence, but now you need a way to compute ln(x) or you can use math.ln(x).

Thanks to your discovery of the upper bound, I can write a solution that (technically) works in 52 bytes.

x=0;while x*x~=C do x=math.random()*395 end return x

;)

/s/ Adam