Posts: 120
Threads: 9
Joined: Aug 2005
Don posed a challenge a time ago
Quote:
OK, here is a challenge. Devise an RPN program or Solver solution to the following problem. Refer to this. Assume this pattern continues forever. I want to input a number, like 19, and the program must tell me the 4 adjacent numbers, like 6, 18, 40, and 20. I see the patterns of square numbers, but I'm not sure this will help me figure out the four numbers I am interested in.
Although I am quite slow in responding I have analysed this problem and developed a HP 17BII+ Solver solution that works for any number greater than 3.
Solution
Select N
Lower square value L = IntegerPart(SQuareRT(N)+0.0001)
The solution will be given for N > 3. The ^ or 'hat' symbol means 'to the power of' and '_' is used as a line continuation symbol.
A number of cases exist leading to different solutions to obtain the adjacent numbers to N
1. N = L^2 (at the square value)
2. N = L^2 + 1 (at the first corner after the lower square value)
3. L^2 + 1 < N < L^2 + L + 1 (between the first corner and the next)
4. N = L^2 + L + 1 (at the next corner)
5. N > L^2 + L + 1 (greater than the next corner)
Let A and C be the vertical results and B and D the horizontal results then for each case
1. A = (L – 2)^2 + 1, B = N +1, C = (L+ 2)^2 – 1, D = N – 1
2. A = N + 1, B = (L + 2)^2 + 2, C = (L + 2)^2, D = N  1
3. A = N + 1,B = (L + 2)^2 + (N – L^2) + 1, C = N – 1, D = (L  2)^2 + (N – L^2) – 1
4. A = (L + 2)^2 + (N – L^2) + 3, B = (L + 2)^2 + (N – L^2) + 1, C = N – 1, D = N + 1
5. A = (L + 2)^2 + (N – L^2) + 3, B = N – 1, C = (L  2)^2 + (N – L^2) + 3, D = N + 1
I have written a HP 17bII+ Solve program to output the results.
ULAM = IF(N<4:0:(L(L:IP(SQRT(N)+0.0001))+L(L2:LxL)+ _
IF(N=L2:L(A:(L2)x(L2)+1)+L(B:N+1)+L(C:(L+2)x(L+2)1) +L(D:N1):0)+ _
IF(N=L2+1:L(A:N+1)+L(B:(L+2)x(L+2)+2)+L(C:B2) +L(D:N1):0)+ _
IF(N>L2+1 AND N<L2+L+1:L(A:N+1)+L(B:(L+2)x(L+2)+NL2+1)+ _
L(C:N1)+L(D:(L2)x(L2)+NL21):0)+ _
IF(N=L2+L+1:L(A:(L+2)x(L+2)+NL2+3)+L(B:A2)+ L(C:N1) +L(D:N+1):0)+ _
IF(N>L2+L+1:L(A:(L+2)x(L+2)+(NL2)+3)+L(B:N1)+ _
L(C:(L2)x(L2)+NL23)+L(D:N+1):0))x0+A+B+C+D)
It is important to check that the brackets match as this may lead to a compile problem or spurious results.
On the menu bar input N and press ULAM. To see the results RCL A, RCL B, RCL C and RCL D.
I hope this may be of passing interest and gives another example of HP 17bII+ programming.
Chris Dean
Edited: 12 Mar 2008, 2:47 a.m. after one or more responses were posted
Posts: 562
Threads: 27
Joined: Oct 2006
Very interesting solution using the square factors.
1 question: should
Quote:
2. A = N + 1, B = (L + 2)^2 + 2, C = (L + 2)^2, D = N
is D=N1?
What is the purpose in adding some 0.0001 to the values.. I saw this in another post,but have not used it myself.
Kind regards,Al
Posts: 120
Threads: 9
Joined: Aug 2005
Allen
The value for D should be N1 as you said and I have edited my post accordingly. Thanks for pointing that out.
The reason I add 0.0001 is to make sure that the square root is rounded up so that when the integer part is used it does not round down say 2.999999999 to 2 when the answer should be 3.
Chris
Edited: 11 Mar 2008, 9:26 a.m.
Posts: 1,392
Threads: 142
Joined: Jun 2007
Chris, this is great. Allen provided an RPL and RPN solution, Gerson provided a BASIC solution, and now you have provided a solver solution. I can't wait to enter it in my 17bii+ and try it out.
I appreciate all the work you (and Allen and Gerson) obviously put into this. It provides a great example of how HP calcs, regardless of the programming platform (RPL, RPN, BASIC, or solver) can solve problems. I have always been interested in how the 17bii can be "programmed" to solve problems, and your code is an excellent example. Thanks again.
Posts: 120
Threads: 9
Joined: Aug 2005
Don
Thanks. I like using the HP 17bII+ as it tends to rely on algorithm development and as I have hopefully shown the code appears to be more meaningful than RPN programming.
Using the Solve functionality is quite limited as sub programs cannot be used however this adds to the challenge!
Chris
Edited: 11 Mar 2008, 10:44 a.m.
Posts: 1,248
Threads: 33
Joined: Aug 2007
Quote:
Using the Solve functionality is quite limited as sub programs cannot be used ...
Yes you can! Perhaps not called a sub program, but nested IF's and the S function accomplish the same purpose.
At least on a 17B, and 27S. I have never used the 17BII+.
Posts: 120
Threads: 9
Joined: Aug 2005
Martin
Thanks for comment. If I had an equation say, A*Sin(Theta) + B * cos(Theta) and I had subprograms for the sin and cos functions how could I use the IF and S function for this? Or could you show me an example of something you have done.
I have read the manual and the IF and S function combined looks rather limited to me. Maybe I am not appreciating the power of the If and S function functionality.
Chris
Posts: 1,392
Threads: 142
Joined: Jun 2007
Chris, maybe I'm wrong (I used MS Word to count 3 times), but I get 48 left parens and 49 right parens. Could you doublecheck? I'm reluctant to start entering this until my parens balance.
Posts: 406
Threads: 47
Joined: Jul 2005
Question. Why is the HP17bII+ not in the library? I'm intersted in the discussion and I don't know anything about the calc.
tm
Posts: 562
Threads: 27
Joined: Oct 2006
Quote:
I like using the HP 17bII+ as it tends to rely on algorithm development...
I like the different solutions above. Can you explain further what you mean about algorithm development? Neglecting the trivial (N +/ 1) solutions, the SOLVER solution above uses 5 decisions (IF statements) to pick between 10 different formulas.
The RPL solution
uses a common answer for the first response (I+2c+3), and a single IF statement to decide the second response, choosing:
I2c+5 if the number is on a side or I+2c+5 if the number is on a corner. Are you saying that a solution that uses 5 IF statements in place of one is more reliant on algorithm development?
I developed the math formulas with pen and paper, and only when completed sat down to translate them into a program. I think both solutions require substantial algorithm development, only the last step of implementation is different.
Posts: 120
Threads: 9
Joined: Aug 2005
Don
There is a typo L(C:B2)) should read L(C:B2) in the third line.
I did not have this extra bracket in my code on the HP 17bII+! I have edited the listing accordingly.
Apologies!
Chris
Edited: 12 Mar 2008, 4:14 a.m.
Posts: 120
Threads: 9
Joined: Aug 2005
Allen
Having programmed all types of calculators over the years I must admit that the HP 17bII+ seems to be pretty challenging. This is why I say that to produce a solution on it relies heavily on development effort. This to me is part of the fun in programming it.
I am in no way decrying yours or anyone elses excellent efforts to produce a solution.
Chris
Posts: 562
Threads: 27
Joined: Oct 2006
I do not think you are.. I am printing your solution so I can study what you did. I just didn't understand the comment :)
Posts: 1,392
Threads: 142
Joined: Jun 2007
Thanks Chris. I'll try it out today.
Posts: 120
Threads: 9
Joined: Aug 2005
I think I phrased my comment badly!
Posts: 1,392
Threads: 142
Joined: Jun 2007
Hey Chris, I entered the code on my 17bii+ silver edition and I don't get the correct results. If I enter n=40, for a/b/c/d respectively I get 168/168/168/19 (only 19 is correct). If I enter n= 119 I get 484/484/484/120. For n=141 I get 572/572/572/142, again only the last value is correct. I have checked the code twice and I think I entered it correctly. My key sequence (after CALC) is: 40 N ULAM RCL (more) A, then it displays A=168. Can you give me some inputs and outputs from your execution? Thanks.
Posts: 614
Threads: 66
Joined: Jul 2006
Hi Don,
I cut and pasted Chris's code into the HP200LX solver. Just had to change the x to * for multiplication. I get the the following results for your data:
N = 19
A = 20
B = 40
c = 18
d = 6
ULAM = 84
N = 40
A = 41
B = 69
c = 39
d = 19
ULAM = 168
N = 119
A = 166
B = 118
c = 80
d = 120
ULAM = 484
N = 141
A = 192
B = 140
c = 98
d = 142
ULAM = 572
Hope this helps.
Bill
Posts: 120
Threads: 9
Joined: Aug 2005
Don
I have checked my listing against the code on my HP 17bII+ and they match. I have also checked a number of results and they are correct. I noticed you said A=168 for N = 40. Actually A should be 41 and the value 168 is the result of A+B+C+D. The soft keys should be ULAM, N, L, L2, B, MORE. Click MORE to get A, C, D.
As an example enter 40 and Press N then Press ULAM, when finished the display should show ULAM = 168. Press RCL B to show B=69, Press MORE to show the rest of the soft keys, Press RCl A to display A=41, Press RCL C to display C=39 and finally press RCL D to display D=19. Press MORE to return to the first set of soft keys.
Some sample results that I got from my HP 17bII+ are
A B C D
40 41 69 39 19
75 114 74 44 76
82 83 123 121 81
91 134 132 90 92
100 65 101 143 99
119 166 118 80 120
141 192 140 98 142
I hope this helps.
Chris
Edited: 12 Mar 2008, 6:07 p.m.
Posts: 614
Threads: 66
Joined: Jul 2006
Hi Chris,
I get the same results on the HP200 as you did with the exception of N=1191.
For N=1191, I get
A=1334
B=1332
C=1190
D=1192
Not sure why the difference.
Bill
Posts: 120
Threads: 9
Joined: Aug 2005
Bill
Sorry I edited the large ones as they where unchecked by me and entered the more reasonable values used by Don.
For 1191 I get A = 1334, B = 1332, C = 1190, D = 1192.
I must have typoed previously (not again!). Sorry I am also watching my team Tottenham Hotspur playing PSV. It has just gone to penalties!!!!!!!!!!!
Chris
Edited: 12 Mar 2008, 6:20 p.m.
Posts: 1,392
Threads: 142
Joined: Jun 2007
Thanks, Chris. I think I found the problem. I was pressing RCL then MORE then A, and that does not work. Apparently, when you press RCL your next keypress needs to be one of the soft keys already displayed, because pressing MORE must confuse RCL and you don't get the proper values. So if you want to see A, for example, you need to press MORE until A is displayed, THEN press RCL A.
I was thinking you could avoid the MORE by using G(L) and G(L2) references everywhere, but you would still come up one spot short. Drat.
Great work.
Thanks for your help.
Posts: 562
Threads: 27
Joined: Oct 2006
Chris,
Thanks again for the different solution. I also had this issue with the RPL solution when I shaved 20 bytes off the program. If you expand the parens and combine terms here:
Quote: 3. A = N + 1,B = (L + 2)^2 + (N – L^2) + 1, C = N – 1, D = (L  2)^2 + (N – L^2) – 1
4. A = (L + 2)^2 + (N – L^2) + 3, B = (L + 2)^2 + (N – L^2) + 1, C = N – 1, D = N + 1
5. A = (L + 2)^2 + (N – L^2) + 3, B = N – 1, C = (L  2)^2 + (N – L^2) + 3, D = N + 1
You will find (as with 3.) That all of the L^2 terms will fall out leaving N+4L+5 or N4L+3 That is much easier to input for your program and may save you some time testing.
3. A = N + 1,B = N + 4*L + 5, C = N – 1, D = N  4*L + 3
4. A = N + 4*L + 7, B = N + 4*L + 5, C = N – 1, D = N + 1
5. A = N + 4*L + 7, B = N – 1, C = N  4*L + 1, D = N + 1
I am keenly interested in how you arrived at the formulas, as you succeeded in something I could not. Namely, that I new that the output is a function of square root of the input, but I could not get there without a RPL loop to count the corner values. Excellent work!
Edited: 12 Mar 2008, 11:40 p.m.
Posts: 120
Threads: 9
Joined: Aug 2005
Allen
Because the spiral has the pattern of squares 'one away' from each corner it seemed to me that there must be an underlying formula. I spent a couple of days (at odd moments) just pen and papering.
I decided that the algorithm only needed to use the lower square root value and that formulae had to be developed for a vertical value and a horizontal values plus special conditions for corners and at the square root.
During the course of doing this I developed conditions for determining all 9 surrounding values. However this was not required for the problem but added a challenging extra to the problem. I did not develop the formulae for each of these conditions and I would definitely not program these into my HP 17bII+ as I think it would stretch the limits of my patience too far! I would possibly use BASIC.
With regards to your 'collecting terms' I did do this but I thought it lost meaning especially when presenting it to others. I also thought that because it is just a straight through solution (no looping) any speed benefits were not worth the loss of readability. I could possibly be wrong about this and as you rightly pointed out it would have made the programming and testing easier.
Thank you for your comments and the interest you have shown in my efforts.
Chris
Posts: 120
Threads: 9
Joined: Aug 2005
Don
I am glad we have got there in the end! As I have previously said I think the HP 17bII+ is a challenging machine to use which adds to the fun of it all!!!!!!!!!!!!!
Chris
Posts: 1,392
Threads: 142
Joined: Jun 2007
I agree, Chris. You and Allen have ably demonstrated how this calculator can do things that the inventor probably never imagined. When I bought my 17bii+ a year ago I never imagined that it could be programmed to solve problems like you and Allen have shown. I personally think it is great to solve nonfinancial problems on a financial calculator!
Posts: 1,392
Threads: 142
Joined: Jun 2007
Trent, the 17B is in the library, and the bii+ is a descendant from that model, as near as I can tell. I think the key layout and basic functionality is the same. Since this is a "museum," only the vintage calcualtors are featured, although anything goes on the forum.
