HHC 2021 Programming Contests - Surprise !
|
10-06-2021, 12:50 AM
Post: #21
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-05-2021 06:38 PM)Gene Wrote: newRPL would have won the day in person had it been used, for sure... as long as the statements in the procedure were standard and could have been put into a 50g, 48gII, etc. :-) That's where it gets weird: the exact same code (for example John Keith's code above), running on different platforms would give you different scores. His code is so standard, it can run on an old 48G, the rubber keyed 49g, on a 50g and then newRPL on a 50g or even newRPL on a Prime G1. All giving you different speeds and different scores for the exact same algorithm... I'm glad I'm not the judge :-) |
|||
10-06-2021, 01:10 AM
Post: #22
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
True. At the conference, the two RPL machines were 50g so no problem there.
|
|||
10-06-2021, 05:24 PM
Post: #23
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Just for fun, I did a 71B BASIC implementation of the RPN problem. This should work on a stock 71B with no additional LEX files or modules.
Rather than searching upward one by one, I took the approach of simply "mirroring" the input number to create a palindrome, checking to see if the result is greater than the input, and if it's not, incrementing the value of the left half by 1 and building a palindrome from that instead. Had to do a few tricks to correctly handle cases with input of all 9. It also works correctly for an input of 0, returning 1. Roughly half of the program is a string-reverse function, and there's room to shave a few more bytes off of this by combining some lines. It's very fast, though. It may look like there's a loop at first glance, but the backward jump should only occur either 0 or 1 times. There is a FOR loop in the string reversal function, of course. Code: 0010 INPUT X |
|||
10-06-2021, 05:35 PM
Post: #24
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Just finished my RPN program using the exact same algorithm Dave just explained ;-)
It doesn't use any synthetics or CX commands. The listing is in 42S code, the corresponding 41 code is 88 bytes without the END. Stack-only, of course ;-) There's probably room for improvement.. Code: 00 { 95-Byte Prgm } Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
10-07-2021, 12:54 AM
Post: #25
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise ! | |||
10-07-2021, 08:14 AM
Post: #26
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Ah of course! Silly me. I used to have a CV+X-Functions, perhaps that's why I assumed it to be a standard function..
Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
10-07-2021, 05:16 PM
Post: #27
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Delete the RTN and save a byte. :-)
|
|||
10-07-2021, 10:50 PM
(This post was last modified: 10-07-2021 10:51 PM by Craig Bladow.)
Post: #28
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Here is my second place entry in the HHC 2021 RPN contest. As submitted it was 72 bytes. I removed the STOP at the end so as presented here it is 71 bytes.
My strategy was to get something working, with some efficiency, and then reduce the program size. My approach compared each digit with its match until failure, so it would not spend time reversing the entire number for every candidate. Also only the first odd number of digits, greater than N/2 (N is the number of digits), would be compared. Very late Saturday I had achieved the first two goals but ran out of time to try any new approaches. Code:
But did I develop the faster solution? I keyed in David Hayden's excellent first place solution, which is a very elegant and compact program and ran Gene's 15151 test number as input. The above routine took 16 seconds to find the next palindrome and David's winning solution took 24 seconds, on the same DM41X, in fast mode, on battery power. So a small consolation! Try CC41! |
|||
10-12-2021, 08:09 AM
(This post was last modified: 10-13-2021 07:40 AM by C.Ret.)
Post: #29
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-07-2021 10:50 PM)Craig Bladow Wrote: I keyed in David Hayden's excellent first place solution, which is a very elegant and compact program and ran Gene's 15151 test number as input. With 15151 as test value, my 57 bytes code takes 0'11"73 to display 15251 on an original HP-41C just because I add a PSE instruction to see progress and a BEEP instruction at end to facilitate time measurement with my watch. I am really surprise that my modest and ageless HP-41C vintage run faster that today hi-tech SwissMicro instruments ! Code: 01►LBL "PAL 1 STO 03 Removing these the instructions PSE and BEEP spare exactly two bytes in code and a bit more than two seconds of running time. (10-06-2021 05:24 PM)Dave Britten Wrote: Just for fun, I did a 71B BASIC implementation of the RPN problem. This should work on a stock 71B with no additional LEX files or modules. Just for fun, I did a BASIC implementation of the RPL problem on a stock HP-71B, not using additional LEX files or JPC ROM module. Code: CAT GOLOMB S BASIC 108 10/04/21 18:54 (108 Bytes) 1 5 -> 27 0'00"33 2 4 -> 17 0'00"26 5 5 -> 9 0'00"33 100 101 -> 882 0'06"44 1000 1500 -> 4884182 1'38"03 2000 2300 -> 5724280 2'30"39 Incidentally, because of natural contradictive mental behavior, I also made a code for the RPL problem on a HP-41C RPN: Code: 01►LBL "GO x=y? FS 02 // Flag 2 is a=b indicator Registers: R00: sum of squares R01 to R###: G(1) to G(###) where SIZE ### is the memory limit depending of your HP-41 configuration. Usage: a [ENTER^] b [XEQ][ALPHA]GO[ALPHA] display \( \sum_{k=a}^{b}G^2_k \) Speed: 1 5 -> 27 0'00"33 1 5 -> 27 0'04"85 2 4 -> 17 0'03"95 5 5 -> 9 0'04"28 100 101 -> 882 1'02"29 1000 1500 -> too few registers 2000 2300 -> too few registers (my HP-41C armed with only two memory modules is limited to SIZE 130 And again, thanks for sharing this contest problems, I learn of few fact by reading all contributions here. I surely have to initiate myself into synthetic programming13 ! EDITED Wed 13-oct 2021: corrected instruction number in last HP-41 code and add bytes count. |
|||
10-12-2021, 03:42 PM
(This post was last modified: 10-25-2021 02:45 PM by Jeff O..)
Post: #30
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Checking this thread for the first time after sleeping on the problem for several nights, working out an approach, starting to write code on a DM42 a few days ago, then fussing with it intermittently until I got a version that worked on all inputs. I first considered the “add 1, see if it is a palindrome, if not, add one and so on” method, but worried that that method could take a while to run on real machines, and surely there was a better way, so I tried to arrive at a direct approach. Eventually I arrived at the method described by Dave B. (Before checking this thread I assumed that I did not come up with a novel method among this group and was not surprised to see it used.) It took lots of tricks (for me at least) to handle all the cases, e.g. even number of digits vs. odd, all nines. The working version is 74 steps, 127 bytes. But I also used the ALENG instruction which I now see violated the rules* so I would have to replace all of those with LOG IP 1 + sequences. But at least it works and I think would be relatively fast on a real 42s since it is a direct approach with no looping required (except a small number of loops to build the palindromes).
Program listing is below. I may try to correct the above and optimize the code (which is quite clunky, I’m sure), if so I’ll post an updated listing. High level comments: Steps 1 through 20 set a flag if the input has an odd number of digits, leaving it clear for an even number, and lops off the rightmost n/2 or (n-1)/2 digits for inputs with an even or odd number of digits, respectively. This leaves an n/2 or (n+1)/2 digit number for even/odd digit inputs. Step 21 calls a subroutine which creates a palindrome by reversing the n/2 digits and tacking on the end for even digit inputs, or reversing the first (n-1)/2 digits and tacking on the end, leaving the rightmost digit (which was the middle digit of the input number) alone for odd-digit inputs. Then compare to the original, if larger, we have succeeded in finding the next largest palindromic number and stop. If equal or smaller, we have not succeeded, so steps 26 through 49 create the next higher palindromic number by incrementing the rightmost digit of the truncated number and creating a palindrome with the subroutine. It took some fussing to enable the routine to handle the "all nines" cases using the method used for all other inputs. It would be simpler and shorter to simply detect that case and add 2 to the input number to find the next higher palindrome. That seems a little "brute-force-y", not as "elegant" as using the same method for all inputs. But is mathematically valid, and I had to handle them as special cases anyway, so it is probably better. Code for that option is provided in the second code block below. I will admit to not having studied all entries discussed above in detail, so if I have used any techniques from those routines, I give full credit to the author and acknowledge priority. I also credit Dejan R. for much of the palindrome building subroutine. While cogitating on my approach, I knew that I would need a routine to reverse digits. Then Dejan handily posted a routine to the HHC group that was easily adapted. I also acknowledge that I worked on it for a week-and-a half, not continuously of course, but not one night as intended for the contest, or the couple of days for other solutions posted above. edit - slightly improved (2 steps shorter) versions developed before I saw Werner's very nice technique that allowed for a significant improvement detailed in a separate post below. Code: 00 { 124-Byte Prgm } Version that simply adds 2 for the all nines cases, "only" 67 steps, 108 bytes: Code: 00 { 108-Byte Prgm } * - Correction, ALENG does not violate the rules, see Gene's post below. Dave - My mind is going - I can feel it. |
|||
10-12-2021, 04:56 PM
Post: #31
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-12-2021 03:42 PM)Jeff O. Wrote: I also acknowledge that I worked on it for a week-and-a half, not continuously of course, but not one night as intended for the contest, or the couple of days for other solutions posted above.Hey, it's not like we, the remote contestants, are in for anything other than the fuzzy warm feeling of pride and accomplishment. Some of us (and that includes myself) try to show off that we would have beat the on-site folks, by solving the puzzle before the deadline, but other than that the deadline really only exists for those guys, not us. On that note, I have two questions for the judge: 1. As part of this show-off, I used to write a post containing the BYTES output of my program (size and checksum) before time was up, but this time I was scared away by the unusually sharp reminder to not only refrain from posting code, but also not post questions ... Would the size be too much of a hint to what's achievable, or can I resume that practice next time? 2. What do the on-site RPL entries look like? I'd like to see what algorithm they used: generating the sequence one-by-one (like John Keith), generating it in same-value stretches (like myself), or something entirely different. |
|||
10-12-2021, 05:55 PM
Post: #32
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Note: ALENG does not violate the rules, as it is part of the extended functions present on the HP-41CX.
|
|||
10-12-2021, 08:45 PM
Post: #33
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-12-2021 05:55 PM)Gene Wrote: Note: ALENG does not violate the rules, as it is part of the extended functions present on the HP-41CX.Obviously I misunderstood something in the thread. That's great, I hate to be a rule-breaker! Dave - My mind is going - I can feel it. |
|||
10-12-2021, 10:30 PM
Post: #34
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise ! | |||
10-18-2021, 05:42 PM
Post: #35
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-06-2021 05:35 PM)Werner Wrote: Just finished my RPN program using the exact same algorithm Dave just explained ;-) Hi Werner, Being an RPN implementation of Dave's method, I was curious about how you did it in fewer steps and bytes than my effort. Of course doing it stack-only helps, eliminating byte- and step-wasting stores and recalls. I'm lazy and usually just store and recall things in registers. (After I get a working program, sometimes I go back and actually see where things go in the stack and try to devise ways to eliminate the use of registers, but have not (yet) done so with my program.) I have not fully dissected your program, but upon loading and running, I did notice that it seems to fail on the "all nine(s)" cases, e.g., entering 9 should yield 11, 99 should give 101, etc. Unless I mis-entered your program, it gives 101 for 9, 1001 for 99, etc. I'm in no way criticizing your effort, your code for these challenges always strike me as marvels of efficiency and cleverness. I assume you simply did not consider these cases. I found them kind of annoying, and handling them was the last thing I added to mine after getting the basic routine to work. I'd be interested in seeing your approach to handling all nine(s) if you care to spend any more time on this problem, which I will understand if you do not. Jeff Dave - My mind is going - I can feel it. |
|||
10-18-2021, 07:44 PM
Post: #36
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
No need to pussyfoot around it: My routine is plain wrong for these cases! Back to the drawing board! Well, once I get back from holiday, that is ;-)
Thanks for pointing it out, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
10-19-2021, 09:53 PM
Post: #37
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-12-2021 05:55 PM)Gene Wrote: Note: ALENG does not violate the rules, as it is part of the extended functions present on the HP-41CX. Following up on this, even thought ALENG was "legal", I created a version using "LOG IP...". I was actually able to create a slightly shorter version using LOG IP instead of ALENG, which pleased me, but the pleasure was short-lived as I quickly found that the program failed for an input of zero, declaring "Invalid Data", due to attempting to take the log of zero. Zero was not test number, and the palindromes are defined as positive integers in the rules, so I do not know if zero, or all negative numbers for that matter, were intended to be valid inputs. If so, the correct output for zero or negative would be 1, probably easiest just to add a test, if less than or equal to zero, just output 1. The "count up until palindrome is found" routines would not have this problem with zero, but would presumably have to test for negatives. Dave - My mind is going - I can feel it. |
|||
10-20-2021, 12:50 AM
Post: #38
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Positive integers are > 0. :-) In my rules.
|
|||
10-20-2021, 06:47 PM
Post: #39
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
(10-20-2021 12:50 AM)Gene Wrote: Positive integers are > 0. :-) In my rules.Well, yes, the palindromes were defined as positive integers. But for the input numbers, the rules only stated that they had to be less than 10^7. Zero and all negative numbers are less than 10^7... Dave - My mind is going - I can feel it. |
|||
10-20-2021, 07:04 PM
Post: #40
|
|||
|
|||
RE: HHC 2021 Programming Contests - Surprise !
Rats... could have had a really tough test case. lol.
Gene |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 8 Guest(s)