lua-users home
lua-l archive

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

Le ven. 5 avr. 2019 à 01:00, Tim Hill <> a écrit :
There is of course the “cheat” answer, which highlights how careful you need to be when setting puzzles:

        local c = 0
        function cin(x) c = c + 1; return ((c % 3) == 0) and sin(x) or x end

Note that if x is a constant, your cin() function will not return a constant value (except when x==0), because it will return x two times before returning sin(x), and we have sin(x)!=x for any x!=0. The problem should have stated that the function cin(x) must return a value that does not depend on any other external or internal variable, but depending only on x, i.e. the mathematical problem should have been to find an "application" and not a "function" (the Lua "functions" are not restricted to be strictly mathematical applications, and can then return values depending on other variables, but the problem was not given as requiring Lua or any specific programming language).

Also the problem does not clearly state the meaning of "smooth". I think it should have been said to be
- an application on a specified input set (e.g. all real numbers representable in the programming language, or a subset of it, but the second part of the problem clearly stated that it must handle input values outside the range where sin(x) has the same sign as the input in radians).
- and the application must be defined, continuous and infinitely derivable on all values of the input range.

Note: the range (-0.7 .. +0.7) is in fact (-pi/4 .. pi/4): on this range all derivate orders of sin(x) have a constant sign and any input of sin(x) in that range will output a value within that range, and with the same sign as the input.

The second half of the problem using x=2.019 is outside the range and the derivates are changing their sign, si it would be difficult to reach the convergence. However, sin(x) has a Taylor development with the almost correct sign if you develop it to the 10th degree to reach the 10-digit precision of the problem and it is enough to compute the Taylor coefficients of cin(x) with a precision of 11 digits for degrees 0 and 1, and 10 digits for degrees 2 or higher.

The coefficient of degree 0 is of course 0. But for getting a warranty of 10 digits of precision of the input, the needed precision for the coefficient depends on the width of the input range: this precision of 10 digits is UNREACHABLE if the input set is (-INFINITE, +INFINITE) and we need a bounded number of Taylor coefficient: with large values of |x|, 10 degrees is not enough for the Taylor polynom. Now suppose you don't precompute the Taylor coefficients but just use an binary search loop, it's not warrantied we get a convergence because the signs are constantly changing for high values of |x|. We can however ensure the convergence if the input is restricted to (-pi, +pi) and then can reply to the problem with x=2.019.

Anyway, this is not really a problem for Lua, any programming language could be used (including an Excel worksheet, or _javascript_, Java, C, C++, C#, VB, PHP, Python, and even Bash or Powershell...) provided they have a numeric floatting point type capable of representing numbers with 10 digits of precision (the IEEE 32-bit "float" is not enough, you need the IEEE 64-bit "double" or better). So it's not really a problem in scope with this list.