My birthday, so a little commemorative minichallenge !  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: My birthday, so a little commemorative minichallenge ! (/thread240057.html) 
My birthday, so a little commemorative minichallenge !  Valentin Albillo  03052013 Hi all, As fate would have it, today's my birthday (don't ask) so I think that a little commemorative minichallenge is in order, namely:
The commemorative minichallengeWrite a short program or code snippet to evaluate a given polynomial at any given particular argument X, without explicitly using either multiplication/division operators (*, /)(that excludes reciprocals as well) or addition/subtraction operators (+, ). For good measure, any and all kinds of conditional, looping, or GoTo statements or constructions are forbidden as well so no equivalents to IF, GOTO, SELECT, CASE, UNTIL, WHILE, LOOP, FOR, ... you get the point. To simplify matters a little you can particularize your code to evaluate this particular 5thdegree polynomial: y = 5 x^{5} + 8 x^{4} + 4 x^{3} + 2 x^{2} + 3 x + 1 for any given argument X (the generalization to arbitrary polynomials is obviously straightforward). Assorted HP models can be used for this challenge (not all can deliver) but in case your particular model has a builtin (or library's) polynomial evaluation functionality (say "CALL POLYEV(coefficients, X)") you're asked not to use it lest you'll ruin the challenge for yourself. I'll give a 3line HP71B solution, which runs like this:
>RUN Try your hand at it, it's actually quite easy !
Best regards from V.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03052013 Hola Valentin, First of all, ¡feliz cumpleaños! I am not sure whether this is what you had in mind, but it does what you want if exponentiation is allowed.
PLEVAL:On my hp 50g: pi PLEVAL > 2463.56024568 Best regards, Gerson. P.S.: This solves your example only, so this is not a solution.
Edited: 5 Mar 2013, 9:49 a.m.
Re: My birthday, so a little commemorative minichallenge !  Thomas Klemm  03052013 Hi Valentin Congratulations!
Here's a solution for the HP42S: 00 { 40Byte Prgm } Not sure whether using \GS+ is allowed. I considered using storage arithmetic kind of cheating.
Kind regards
Edited: 5 Mar 2013, 10:24 a.m.
Re: My birthday, so a little commemorative minichallenge !  Walter B  03052013 Feliz cumpleaños, Valentin! I don't want to spoil your challenge  just want to make sure I got it right: we are allowed to use every calculator key except [+], [], [*], [/], and [1/x], aren't we?
d:I
Re: My birthday, so a little commemorative minichallenge !  Gilles Carpentier  03052013 Hi Gerson A little improvment :
Happy birthday Valentin ;) Edited: 5 Mar 2013, 12:23 p.m.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03052013 Hi Gilles, Thank you! I had never heard of AXL (array <> list) before. This was new to the 49G series, as I've found out in the Advanced User's Guide. Gerson.
Edited: 5 Mar 2013, 12:38 p.m.
Re: My birthday, so a little commemorative minichallenge !  Eddie W. Shore  03052013 Happy Birthday Valentin! :) May it be a good one.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03052013 I guess SEQ belongs in the looping category, thus forbidden. Also, using LN EXPM to subtract 1 might be cheating. Anyway, here is my generalization attempt: PLYEVAL: _{Edited to fix a typo.}
Edited: 5 Mar 2013, 4:16 p.m.
Re: My birthday, so a little commemorative minichallenge !  Massimo Gnerucci (Italy)  03052013 Happy birthday, Valentin! :)
Re: My birthday, so a little commemorative minichallenge !  Gilles Carpentier  03052013 A generic solution 65 Bytes for 50G
« OVER AXL 1 « DROP ENDSUB NSUB  » DOSUBS ^ AXL DOT »
entry is for example [ 5 8 4 2 3 1 ] 7 Re: My birthday, so a little commemorative minichallenge !  Csaba Tizedes (Hungary)  03062013 Hi Valentin, Happy Birthday to You! :) So, we livin in a material world, therefore my solution according to this:
1.) Grab your favourite Financial calc, clear the CF regs This works x=PI, but not for x=7... :(
Csaba
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Gerson ! Thank you very much for your "felicitación", much appreciated. And yes, your solution (which it actually *is*, as I stated that evaluating the specific polynomial was sufficient) is just what I had in mind, see my original solution below.
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Gilles: Thank you very much. Nice, elegant, short solution, by the way, just what I had in mind (see my original solution below). I knew that it was easy but expected it to last a few hours and survive a few attempts ... :D
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Thomas: Thanks a lot for your congratulations, much appreciated. Your attempt is nice and short but regrettably it uses the GS+ which, having a "+" sign and being essentially an explicit sum (much as STO+, etc), is not allowed per the minichallenge requirements. But thanks anyway for submitting it and for your interest.
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Walter:
Quote:
As per your question, yes, the explicit symbol operators for the four arithmetic operations (plus reciprocals) aren't allowed.
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Eddie ! Thank you very much for your kind congratulation. And yes, it was a perfectly fine birthday, with cake and little candles and all that jazz ... :)
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Nice problem. How to get multiplies and adds. Rectangular to polar conversion gives sqrt(x^{2} + y^{2}). With lots of squaring and square roots this will give a kind of addition and logs and exponentials would produce multiplication. However, it won't handle negative numbers properly. Another approach then. Complex multiplication would work but that is a multiply which isn't permitted. How about a dot product instead? That does work. A WP34S solution is based around this sequence which performs one step in the Horner expansion. It effectively calculates sum*x + B with the current sum in X and the x value in L.
#001 Then an altered first sequence and any polynomial can be done. The example quintic follows:
001 LBL A All the short integer constants aren't required, although steps 2 through 4 need to be separated with ENTER if not.
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013
Now to begin the 2nd half of my 6th decade ... (2,6 > 26 = 13 + 13 ).
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Quote:
Best regards from V.
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Csaba: Thanks a lot for your congratulations, much appreciated. Very nice solution, indeed. Re your technique, I actually wrote many years ago an article on generic Nthdegree polynomial root finding and evaluation in the HP financial model par excellence, the HP12C, namely: Congratulations to you for using this littleknown capability, well done !
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi, Paul: Brave attempt, which stumbles upon the right idea (using DOT) and implements it as well as it can without the recourse to vector or matrix operations, which I understand aren't available in this particular model, nor loops, which are disallowed by the minichallenge's requirements. Well done.
Best regards from V.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Another thought. On the 34S we have a good selection of percentage calculations. Specifically:
The first allows multiplication of x and y. It also allows a rescale by 0.01 if either x or y is unity. The second gives division which we don't need here. More importantly, it allows multiplication by 100 if y is 1. Combining this with the above gives us straight out multiplication. A * B is calculated with a sequence like this:
A The third gives us subtraction when combined with the first, providing A is not zero. If A is zero, we've got CHS that does the same job. A  B is calculated via:
B At this point any polynomial can be coded via Horner's method, albeit rather painfully.  Pauli
Edited: 6 Mar 2013, 5:06 a.m.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 The 34S does have matrix operations, although they are a little clumsy. Remember those really ill conditioned matrices you presented last year? Unfortunately, all the relevant matrix operations include + or * in their names. That leaves us with the LU decomposition, determinant and inverse. The library has a few more: trace & absolute trace, row and column norms and the Frobenius norm. It might be possible to solve this challenge using these. There are also some 3D vector routines in the library but only the dot product is usable without encroaching on the no arithmetic operators rule. This isn't really different to the solution above. I'm also loathe to include the library functions in solutions.  Pauli
Edited: 6 Mar 2013, 5:54 a.m.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Extending this line of thought and using percent change it is possible to get a sequence that works on a 12C. The scientific models really are left behind this allstar calculator. Remember the formula for percent change:
This can be used instead of %MG above provided y is not zero. If y is zero, then we don't need to subtract anything and can skip that step. The sequence to get A  B is therefore:
B
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Happy birthday and thanks for this interesting minichallenge.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 To prove the 12C can solve this one:
01 STO 0 I suspect that with a lot more pain, the use of the register could be avoided as well. I don't know if the 12C has enough memory to hold the resulting program however.
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013 Hi again, Paul: Thank you very much for your congratulations, I really appreciate it. Also, thanks for your interest in this minichallenge and for your posted attempts, which are of great interest themselves. As I think the 34s does include full financial functions, they can be used as well to solve it, as explained in the posts above.
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 The 34S doesn't have full financial functions. It is deliberately lacking here, we never intended it to be a financial calculator. It does, however, have a very good selection of percentage calculations and a robust TVM solver in the standard library. The fact that a 12C can solve this challenge is great and actually caught me a bit by surprise. Never underestimate where arithmetic operations are hiding in other commands. I suspect the least featured scientific calculator that can solve this challenge is the 15C. At least I can see how to do it on the 15C using MATRIX 6 and I can't (yet) see how to do it on a 12C or 32S. I'd love to be proven incorrect here. Some possibilities to consider remain:
Still, I'm sure other people will come up with some more intriguing possibilities.
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03062013
First of all, thanks a lot to all of your for your interest and clever solutions to this commemorative minichallenge.
My own original solution is the following 3 lines of HP71 B code: 1 DESTROY ALL @ OPTION BASE 0 @ DIM A(5),B(5) @ READ A
A few comments:
Best regards from V.
Re: My birthday, so a little commemorative minichallenge !  Walter B  03062013 Quote:FYI, it only contains a small subset. There is no lack of reasonable financial calculators in this world (maybe there's a lack of reasonable persons consciously operating them but that's another topic) so we focussed on scientific functions. What Pauli mentioned is pretty much all of the %functions supported. If you want to know more about it, please look here (8.7MB) or get the book.
d:)
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Maybe an integer mode solution is a possibly. Addition can be done via basic logical operations xor, or and and plus some shifts or rotates. From this multiplication by a constant is possible via repeated additions. You know what the constant is so you can inline that many additions. There are better algorithms but I don't see how to implement them branchlessly. It would be truly horrible without subroutines or loops but I think it is theoretically possible.
 Pauli
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03062013 Quote: I had considered R>B #1h XOR B>R instead of LN EXPM IP on the HP 50g, but I quit because the former requires more bytes.
Gerson. Edited: 6 Mar 2013, 6:54 a.m.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 Not even close to them all :) There are three more:
% x * y / 100 In an earlier iteration we also had:
%+ y + x * y / 100
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 The integer code sequence is wrong. It converts 2 to 3 and 3 back to 2. It isn't counting. I do like LN EXPM to count. I'm sure there are sequences of logical operations that will do the add. RJ justifies the number and returns a count of how many zeros it skipped over. That could then be used as an argument to a mask or shift command to get a decrement operation. You want to set the lowest 1 bit to zero and all zero bits below that to one. Something like this sequence will subtract one:
RJ I suspect a parallel adder is a possibility and would be shorter than a sequence of these.
Re: My birthday, so a little commemorative minichallenge !  aurelio  03062013 Happy birthday Valentino excuse me for the late and thank you for your challenge idea, it has been a chance for me to take one more lesson in programming technique by the "GOTHA" in this matter , here on this wonderful board.
Aurelio Edited: 6 Mar 2013, 8:12 a.m.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03062013 Quote:
Same on the the HP42S: 1 3 XOR > 2
It's obvious now only odd numbers will be subtracted one unit this way. Gerson.
Edited: 6 Mar 2013, 8:21 a.m.
Re: My birthday, so a little commemorative minichallenge !  Walter B  03062013 Ok, ok, I stand corrected :) Still I think [Delta]% and %MRR are the only two really useful, maybe also the margin calculations to some extent. The other ones I'd do faster manually than looking them up in X.FCN.
d:)
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03062013 That is a nice little subtract one sequence. Only works in two's complement or unsigned modes and doesn't work for negative numbers.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03062013 NOT +/ will always add one to integers. Useful only to overcome no + restrictions, I think.
Gerson.
Re: My birthday, so a little commemorative minichallenge !  Paul Dale  03072013 This works for twos complement and unsigned, but not the other two integer sign modes on the 34S or ones complement on the 16C. Still it is a nice way to avoid +.
Re: My birthday, so a little commemorative minichallenge !  nina scholz  03072013 LBL A Edited: 7 Mar 2013, 3:33 a.m.
Re: My birthday, so a little commemorative minichallenge !  Valentin Albillo  03072013
I'm glad you've found my commemorative minichallenge interesting and perhaps you might consider the techniques discussed as an useful addition to your programming knowledge chest.
Best regards from V. Re: My birthday, so a little commemorative minichallenge !  Walter B  03072013 Please see this post above.
Re: My birthday, so a little commemorative minichallenge !  Gerson W. Barbosa  03112013
Quote:« OVER AXL 1 « DROP ENDSUB NSUB  » DOSUBS ^ AXL DOT » Very nice and elegant! Better ignore the plus, minus & times restrictions, now that the minichallenge is over. The generic solution below is 111 bytes long, but avoids exponentiation (should be faster and more accurate for higher order polynomials). It looks a bit clumsy, however. Any idea for a shorter, faster and elegant solution that uses no exponentiation?
%%HP: T(3)A(D)F(,); 
%%HP: T(3)A(D)F(,);This returns 1.01465154025E17 in 2.14 seconds on my HP48GX (with Erable & Metakernel), only slightly faster than your sizeoptimized code (2.33 seconds). Builtin PEVAL is about eight times faster.
Edited: 11 Mar 2013, 10:36 p.m.
