lua-users home
lua-l archive

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


also this solution still uses the exact value "x^x" which is the searched value; as we dont know it first, we can pass it as a second parameter y to function f
So to get the value it's a bit more complex:

function f(x,y)return(x*math.log2 x div 512)*y, y end
(54 bytes)
and to get the value just call "f f f f f f(x 3)" where we provide the initial approximation

And do we need the explicit "return" statement, can't the function use recursive call to itself, something like:

   function g(x,y)(x*math.log2 x div  512)*y,y end  
   function f(x)g g g g g x,3 end
   and get result with just "f x"
(untested, I've probably oversimplified the dependancies, but I must not be very far from the final solution, may be we can drop the second parameter by assuming oit is in a global constant, by transforming a bit the formula inside g)

Now we also need to "optimize" the code size for "math.log2" and the constant "512".
That's where the global constant C=15.5 may play a role, allowing us to use the standard log function (but its name may also be in a global to avoid access to "math.").

The bit fiddling used may also be implemented using the "%2" (modulo 2) operator instead (to replace the initial "and 1 or 2" condition which just needs 1 bit of precision to test if we pass the lower bound or the upper bound of the new approximation when comparing to the value 1024)

There are several tracks to analyse.


Le jeu. 27 juin 2019 à 18:43, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :


Le jeu. 27 juin 2019 à 18:30, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :
i.e. it's enough to pass to the recursion call the value x^x*(1+(log2(x)*x<1024 and 1 or 0))
correction: pass the value x^x*(x*log2 x<1024 and 1 or 0)
So you get an anwer:
f f f f f f(x^x*(x*log2 x<1024 and 1 or 2))

I think that (x*log2 x<1024 and 1 or 2) can be implemented using an integer division to remove the "and"/"or":
(x*math.log2 x div 512) 

And we get something like:
f f f f f f(x^x*(x*math.log2 x div 512))

function f(x) return
x^x*(x*math.log2 x div 512)
end

and to get the value just call "f f f f f f x"