![]() |
A simple wp34s mini-challenge - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: A simple wp34s mini-challenge (/thread-185983.html) |
A simple wp34s mini-challenge - Gerson W. Barbosa - 06-12-2011 Quoting from the Wikipedia article on Ramanujan: "Imagine that you are on a street with houses marked 1 through n. There is a house in between (x) such that the sum of the house numbers to left of it equals the sum of the house numbers to its right. If n is between 50 and 500, what are n and x?" Other HP RPN calculators can be used, but only on the wp34s this can be done in 18 steps or less (LBL and RTN included) using the method I have used. It is possible Ramanujan's method based on continued fractions allows for an even smaller program, but I am not aware of it. A solution based on his method is much welcome, no matter the program size, in case anyone knows it.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 Interesting challenge. I've no idea about the continued fraction solution. I suspect the 16c can do this in 18 steps too although I might be wrong here. Anyway the 34s can do a bit better than that:
001: LBL A This requires the device to be in integer mode with a sufficient word size (19 or 20 minimum I think -- I used 64) and decimal base. Press A and x is returned in the display. n is in register I. If you don't allow an integer mode start, change steps 2, 3 & 4 to:
002: 5 Actually, the base doesn't matter here. We just need to be in integer mode. I love the S.L and S.R commands :-) How did I arrive at this? A little algebra. The sum of the numbers to the left of x is x (x-1) / 2. The sum of the numbers to the right is (n + x + 1) (n - x) / 2. From this you get that n (n+1) / 2 = x^2 at the solution both sides must be integral. The divide by two is safe since n^2 - n is always even. So we need to check the square root for being integer. Fortunately integer mode square root sets carry if there is a remainder. - Pauli Edit: return n in register I instead of n+1.
Edited: 12 June 2011, 9:22 p.m.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 And an improvement. Replace lines 2 through 4 with MASKR 09 giving a 12 step solution:
001: LBL A entry label
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-12-2011 Very quick solution! I wonder if Ramanujan was as fast :-) I have not used a good method, it would have taken too long if done by hand (too many quadratic equations to solve). Anyway, it works:
sum of the house numbers to left: x*(x-1)/2 Regards,
Gerson.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 If we can input the loop starting value before running the program...
001: LBL A 50 A to execute. x is in the display. RCL I for n.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 And a version that doesn't use a register:
001: LBL A As above, 50 A to run. x in in the display. X<>Y to get n.
001-43,22, A LBL A I thought it could be done.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 If you are going to try the 16c version, remember to set the word size large enough. And start with a guess for n around 250 or you'll be waiting and waiting and waiting...
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-12-2011 Only 100 seconds to find the answer, starting from 250, WS set to 20. Fast enough! My having resorted to SLVQ looks really silly, that's what happens when we stick to the first idea :-) Cheers,
Gerson. Edited: 12 June 2011, 11:06 p.m.
Re: A simple wp34s mini-challenge - Paul Dale - 06-12-2011 I thought about using SLVQ too but figured the setup would be too expensive in steps. Then I remembered that integer square root did the FP test for me...
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-12-2011 Quote: Yes, we CAN! Sorry for not having warned you about that.
Gerson.
Re: A simple wp34s mini-challenge - Paul Dale - 06-13-2011 For the continued fraction solution to this problem have a look here. I'll leave a program to calculate this as an exercise :-)
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-13-2011 For solving, I'll choose Dario Alpern's solver. A program based on continued fractions would be a bit larger but would run significantly faster. The general solution for 2*x2 - y 2 - y = 0 is
xn+1 = 3*xn + 2*yn + 1 Here is a 42S program to display the solutions:
00 { 44-Byte Prgm } Notice the solutions are successive approximants to sqrt(2), when divided by each other. They used to solve this kind of problem centuries ago, only it took a little longer :-) http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv018.cgi?read=145904 (I knew I'd seen this before...) Gerson.
Edited: 13 June 2011, 3:14 a.m.
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-13-2011 Thanks for the link! The trick is to transform the equation we'd already found
m2 + m - 2n2 = 0
into an easier to solve Pell's equation: m2 + m = 2n2 Now the problem is ready to be quickly solved by the following HP-42S program:
00 { 70-Byte Prgm } 27 1/X This will solve Pell's equation for any positve integer N, such that N is not a perfect square, as long as machine accuracy allows it:
Generic Pell's Equation Solver ( x2 - N*y2 = 1 ) I'll leave optimization and porting to wp34s as an exercise :-) Gerson. P.S.: Be warned this is my own algorithm for generating the approximants to sqrt(N), which means there is quite room for improvement.
TurboBDC code
Edited: 13 June 2011, 9:31 p.m.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-14-2011 Hi Gerson, if you want it really fast, then try this one on your 42S(or on the WS34). This program will stop and show matching "n" and "x" in Stack y and x. Continue with R/S to see the next pair. I've left out the "50" and "500" condition. You'll see why :-)
00 { 44-Byte Prgm } Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-14-2011 Wow! Really fast and optimized! And you can optimize it even further if you replace lines 12 and 13 with
12 STO+ ST X This will save one byte and one step. Thank you for your valuable contribution!
Gerson.
Re: A simple wp34s mini-challenge - Gene Wright - 06-14-2011 I believe the 34s will require the first few steps to be 0 ENTER 1. The 34s stores numbers one digit per line and so 0 1 is just 1. That will be something to watch out for if converting programs from the 42s / 41c to the 34s.
FYI.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-14-2011 Hi Gerson and Gene,
your comments made me delete some lines. But now the values 0 and 1 must be on the stack. I have also added some comments, because I think the code is rather kryptisch :-) A_new = A_old + B_old ; B_new = B_old + 2*A_old x=A*B n= MIN(2A^2;B^2) put 0 and 1 on the stack and then XEQ "S"
00 { 39-Byte Prgm } Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-14-2011 Günter,
I'd rather keep the values 0 and 1 inside the program. It is possible to achieve this on the wp34s using two steps. That's a good reason to define 0^0 = 1 :-) 001 LBL B Regards,
Gerson.
Re: A simple wp34s mini-challenge - Paul Dale - 06-14-2011 There is another way, in fact lots of ways.
iC 00 or
CLSTK
Pity I didn't define complex versions of the internal constants -- then it would be possible in a single step :-)
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-14-2011 Quote: Missed that one. It has the advantage to save yet one more byte in the HP-42S program.
Gerson.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-15-2011 As I'm aiming more for the 42S and here for the Free42, I'd like to leave the input outside of the program, not only would the inclusion add 2 superficial lines it also required an additional local label. When playing around to get this piece done I didn't really squeeze the last byte out of it, because my main problem was to recall something I dealt with 25 years ago, when I ran into a similar mini-challenge. "Scrooge" didn't remember how rich he was, but knew he had something between 500.000 and a million Golddollars, and they could be layed down either as a square or a triangle. Like in this mini-challenge there was a lot discussion how to optimize the necessary loops. Then I came with my 1 line BASIC program and the discussion was over. okay, here is my next optimization, with a slightly different approach, and some lines less :-) put 0 and 1 on the stack, then XEQ "S"
00 { 33-Byte Prgm } BTW, I'm not at all an expert in math. The solution is based on simply analyzing the figures found with brute force that time 25 years ago. The hint to the continoues fraction approach now sheds some light on my findings.
Günter
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-15-2011 Quote: Yes, that's essentially the same problem as it can be described by the same equation, m2 + m = 2n2. Are you sure the specified range is correct? Here is a brute-force one-liner:
1 FOR I%=1 TO 1000: R=SQR(I%*(I%+1)/2): IF (R-INT(R))<>0 THEN NEXT ELSE PRINT I%*I%: NEXT
Quote: Neither am I.
Quote: Very insightful analysis, I would say. Congrats! Gerson.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-16-2011 Hi Gerson
Quote: You would need R to be squared. I% is the base of the triangle, thus the results are wrong. concerning the boundaries, I now believe the upper limit of "Scrooge's" dollars was two millions.
Günter
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-16-2011 Oops! Forgot to check the results. It should have been
1 FOR I%=1 TO 1700: R=SQR(I%*(I%+1)/2): IF (R-INT(R))<>0 THEN NEXT ELSE PRINT R*R: NEXT
Gerson.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-23-2011 Hi Gerson
so here is the next condensed Free42 approach, that's really short. clear the stack and then XEQ "T" R/S will show the next pair of m and n
00 { 30-Byte Prgm }
Günter
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-26-2011 Really impressive! On the 34s you would need only 11 steps, or 13 if you wanted to press A and R/S for each pair:
001 LBL A Gerson.
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-27-2011 001 LBL A This will generate the sequence
1, 6, 35, 204, 1189, 6930, 40931,... Re: A simple wp34s mini-challenge - Paul Dale - 06-27-2011 010 STO ????
Re: A simple wp34s mini-challenge - Gerson W. Barbosa - 06-27-2011 STOP Too cold here right now (6 °C), hence the typo :-) Gerson.
--------------- 001 LBL A Edited: 27 June 2011, 8:29 p.m.
Re: A simple wp34s mini-challenge - Guenter Schink - 06-29-2011 Hi, as the 42S doesn't have the INC nor the BACK commands, I think there's little room, if at all, to make that code shorter. I think I leave this how it is now.
|