Post Reply 
HHC 2021 Programming Contests - Surprise !
10-03-2021, 11:35 PM (This post was last modified: 10-05-2021 07:51 PM by Didier Lachieze.)
Post: #4
RE: HHC 2021 Programming Contests - Surprise !
[Edited according to the comments from Gene, here]

My RPN solution in 35 bytes (31 bytes if the 41 is configured with FIX 0 and CF 29, and you remove these two instructions before LBL 01):

Code:
00 { 35-Byte Prgm }
01▸LBL "PAL"    // Program must be called PAL
02 FIX 0        // FIX 0 and CF 29 needed to ensure ARCL will put only 
03 CF 29        //  the value of X without decimals nor separators in Alpha
04▸LBL 01
05 1
06 +            // Start looking for palindrome from the next number
07 CLA          // Clear Alpha register
08 ARCL ST X    // Put current number in Alpha (up to 7 digits as the number is < 10^7)
09 SF 25        // Set error flag to ensure next error will not stop the program
10 1            // One byte value as key code for PASN, it will not be used and is invalid 
11 PASN         // This instruction has been chosen as it will put up to seven characters
                //  of the function name in Alpha into the scratch register Q in reverse order
                //  so reversing the digits of the number we put in Alpha at step 08.
                // The number in Alpha is searched within the existing functions and program names,
                //  as it is not found it should generate a NONEXISTENT error message,
                //  but it doesn't since flag 25 is set. If there was a global label identical to the
                //  name in Alpha, the invalid keycode in X ensures no assignment is done, and flag 25
                //  ensures that the execution continues.                
12 RCL Q        // Recall the reversed number (in alphanumeric form)
13 STO M        // Store it in the first 7 characters of Alpha, replacing the previous value
14 ANUM         // Convert the reversed number in Alpha to it's numerical value in X
15 R^           // Get the initial number
16 X≠Y?         // Compare it to it's reversed form 
17 GTO 01       // If the two numbers are different, it's not a palindrome, goto next number
18 END          // The two numbers are identical, we have found the next palindrome

There are two synthetic instructions at step 12 and 13 that I've entered with the Zenrom module as this is one of the simplest way to enter synthetic instructions.

This program is pretty slow as for each loop there is a search for a non existent label but it works for input numbers less than 10^7 as specified for the contest.

It is based on the REV program in the Q-loader section of Wickes' Synthetic Programming book, page 57, where I've saved a few bytes by using PASN instead of GTO IND M.

Code:
01 LBL "REV"
02 ASTO M
03 SF 25
04 GTO IND M
05 RCL Q
06 STO M
07 END

The program REV uses the fact that the Q register is used as a scratch register by the 41 OS to store - in reverse order - strings that are usually entered by the user, for example as functions names. The program REV uses 'GTO IND M' to avoid having the user entering the string, but this GTO IND requires the label name to be first ASTO'ed in a register (here M is used, the first register of Alpha), so only strings of 6 characters can be used as the first byte in the register is used to indicate that the register contains a string and not a number.

To be able to reverse 7 digits numbers I initially used ATOX and XTOA:

Code:
01 LBL "REV"
02 ASTO M
03 SF 25
04 ATOX
05 GTO IND M
06 RCL Q
07 STO M
08 X<>Y
09 XTOA
10 RDN
11 END

Then I looked at the different functions of the 41CX that use a user-entered string, and found that with PASN which uses a string in Alpha the Q register was directly loaded with 7 digits from the Alpha register in reverse order, so no more need for ASTO, nor ATOX and XTOA.

Reference: Sections 4B. THE ALPHA REGISTER, p31, 4C. REGISTER Q, p33 and 5I. The Q-LOADER, p55 in Synthetic Programming on the HP-41C by W.C. Wickes.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2021 Programming Contests - Surprise ! - Didier Lachieze - 10-03-2021 11:35 PM



User(s) browsing this thread: 9 Guest(s)