Re: [OT?] Mini-challenge Message #29 Posted by Eduardo Duenez on 4 Mar 2008, 11:57 p.m., in response to message #1 by Eduardo Duenez
Congratulations to Valentin and Paul Dale on independently finding solutions to the challenge.
The solution I found is identical to the one posted by Valentin, at least as far as I can tell. The main identity is:
1/(1/(x-1)-1/(x+1)) + 1/2=x^2/2
This is valid for all x except 0, 1, -1.
As remarked by Valentin, this formula is numerically bad when x is either very close to 0 or large in absolute value.
To go from this to the product xy we use the following "polarization identity" (very useful in the theory of inner products from linear algebra):
xy = ((x+y)^2-x^2-y^2)/2
The two formulas above could be combined into a single, rather long one valid whenever x, y and x+y are neither 0, 1, nor -1. If either of these occur then the value of u^2/2 has to be handled separately.
The following HP 35s program handles all the cases (but does not deal with issues of loss of numerical accuracy). Moreover, it is written in such a way that, from the point of view of the stack, it works just like the multiplication key, that is, x,y are taken from the stack, and their product is returned to the X-level without disturbing the original contents of the Z & T registers. To achieve tis goal it uses STO/RCL arithmetic extensively (but only the +/- operations so, in principle, everything could be done just adding and subtracting plus possibly keeping track of intermediate results on paper or in memory).
The listing follows
S001 LBL S
S002 x=0?
S003 GTO S031
S004 x<0?
S005 +/-
S006 STO M
S007 CLx
S008 1
S009 RCL- M
S010 x=0?
S011 GTO S033
S012 +/-
S013 1/x
S014 STO S
S015 CLx
S016 1
S017 RCL+ M
S018 1/x
S019 STO- S
S020 CL x
S021 RCL S
S022 1/x
S023 STO S
S024 CLx
S025 2
S026 1/x
S027 STO+ S
S028 CLx
S029 RCL S
S030 RTN
S031 STO S
S032 RTN
S033 CLx
S034 1
S035 STO S
S036 RTN
P001 LBL P
P002 STO X
P003 x<>y
P004 STO Y
P005 +
P006 XEQ S001
P007 STO P
P008 CLx
P009 RCL X
P010 XEQ S001
P011 STO- P
P012 CLx
P013 RCL Y
P014 XEQ S001
P015 STO- P
P016 CLx
P017 RCL P
P018 RTN
XEQ S replaces x with x^2/2 and also stores this in S (uses M for temporary storage). LN=112, CK=8715.
XEQ P takes x,y and returns xy and stores it in P (also uses variables X,Y for temporary storage). LN=54, CK=7B85.
I hope you found this problem interesting. Though I mostly see it as a challenging exercise in algebra, the solution nonetheless illustrates beautifully how to go from a particular case (squaring) to the general one (multiplying arbitrary numbers).
Eduardo
|