Post Reply 
HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
09-12-2022, 02:53 PM
Post: #21
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Here is a version for the HP-28 and 48 only, it would need to be modified for the HP 49/50 because of decimal points in approximate numbers.

Code:

\<< 0 SWAP                  @ Counter = 0
  DO DUP NREV +             @ Reverse and add
    SWAP 1 + SWAP           @ Increment counter
  UNTIL DUP2 \->STR "E" POS @ Is >= 10^12?
    SWAP 50 \>=             @ Count >= 50?
    IF OR
    THEN DROP2 0 DUP 1      @ Replace with two zeros
    ELSE DUP DUP NREV SAME  @ Exit main loop if palindrome
    END
  END
\>>

It calls the following program NREV which reverses the digits of an integer.

Code:

\<< \->STR "" OVER SIZE 1
  FOR k OVER k DUP SUB + -1
  STEP SWAP DROP STR\->
\>>
Find all posts by this user
Quote this message in a reply
09-12-2022, 03:15 PM
Post: #22
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
An illicit non-entry for the 33s, probably faster than 50g User programmes:

Code:
1.    LBL C
2.    STO C
3.    XEQ R
4.    RCL+ C
5.    STO C
6.    CLx
7.    STO D
8.    RCL+ C
1.    LBL D
2.    5.E11
3.    –
4.    x<0?
5.    GTO E
6.    CLx
7.    ENTER
8.    RTN
1.    LBL E
2.    SGN
3.    STO- D
4.    CLx
5.    RCL C
6.    XEQ R
7.    RCL- C
8.    x=0?
9.    GTO F
10.    CLx
11.    RCL C
12.    RCL+ R
13.    STO C
14.    GTO D
1.    LBL F
2.    RCL+ D
3.    RCL C
4.    RTN
1.    LBL R
2.    STO N
3.    CLx
4.    STO R
1.    LBL S
2.    CLx
3.    10
4.    STO* R
5.    RCL N
6.    x<>y
7.    RMDR
8.    STO+ R
9.    CLx
10.    RCL N
11.    LASTx
12.    INT/
13.    STO N
14.    x≠0?
15.    GTO S
16.    RCL+ R
17.    RTN

C: LN = 24
D: LN = 36
E: LN = 42
F: LN = 12
R: LN = 12
S: LN = 63
Total LN = 189
Find all posts by this user
Quote this message in a reply
09-12-2022, 03:17 PM (This post was last modified: 09-12-2022 03:19 PM by Gerald H.)
Post: #23
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
A possibly licit entry for 50g but is more verbose than allowed by rules:

Code:
Size: 153.50

CkSum: # 5EFFh

::
  CK1&Dispatch
  # FF
  ::
    DUP
    "Seed"
    >TAG
    "Iteration "
    BINT0
    4ROLL
    DUP
    FPTR2 ^Z>S
    'EvalNoCK:_sup_
    xSREV
    FPTR2 ^S>Z
    BEGIN
    FPTR2 ^QAdd
    SWAP#1+SWAP
    3PICK3PICK
    #>$
    &$
    BIGDISPROW1
    DUP
    FPTR2 ^Z>S
    DUP
    BIGDISPROW3
    'EvalNoCK:_sup_
    xSREV
    DUP
    DISPROW5
    FPTR2 ^S>Z
    2DUP
    EQUAL
    UNTIL
    DROP
    3UNROLL
    FPTR2 ^#>Z
    SWAP
    >TAG
    SWAP
  ;
;
Find all posts by this user
Quote this message in a reply
09-12-2022, 08:03 PM
Post: #24
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-12-2022 12:49 PM)3298 Wrote:  This part is a subprogram because it's used in two places: adding the reversed onto the original, and checking for a palindrome (the latter is implemented as an equality check between reversed and original).

Same idea. Better keep the program in one piece, like you did, even though the length increases by some 10% (# 559Ah, 221 bytes).

Code:
 « R→I -105 CF 0 SWAP
  « →STR { "0" } 1 PICK3 SIZE
    START OVER HEAD + SWAP TAIL SWAP
    NEXT NIP "0" OVER TAIL REVLIST +
  » → h
  «
    DO h EVAL ∑LIST OBJ→ SWAP ∑LIST OBJ→ + SWAP 1 + SWAP DUP h EVAL
    UNTIL == PICK3 50 ≥ OR
    END OVER 50 == { DROP2 0 DUP } IFT
  »
»
Find all posts by this user
Quote this message in a reply
09-12-2022, 08:30 PM (This post was last modified: 09-12-2022 08:33 PM by C.Ret.)
Post: #25
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Ha, Ha !

I finally and not without difficulty, succeeded in adapting my code to the rules of the game. I used an algorithmic shortcut which made it stop when the user entered a palindromic integer.

I thought I was going crazy; I turned like a fool any subsets of my code in all the directions and , the only versions that worked by correctly applying the rule were too long, too slow too complex.

It is only in the evening that I had the idea of using the user flag FLG 00 and I was able to adapt my original version cheaply.

The code I obtained is very similar to Werner's, at least in its operating principle.
On the other hand, the calculations made and the registers used have nothing comparable. In fact, it is greatly different in the details and a bit longer code:

51 bytes (without global alpha LaBeL and END), three registers and a flag.

(09-12-2022 01:51 PM)Werner Wrote:  My first try, 41-compatible, needs 1 register (00 here)
41: 42 bytes without LBL and END.

Code:
00 { 54-Byte Prgm }
01▸LBL "HHC2022"
02   STO 00  STO- 00  XEQ 10
05  ▸LBL 03                                                      @ main loop
06     +  XEQ 10  ISG 00  X<>Y  X≠Y?  GTO 03
12   RCL 00  X<>Y  RTN                                     @ end

15▸LBL 10                                                        @ reverse number X -> revX X
16   ENTER  ENTER  -  10  LASTX
21  ▸LBL 02
22     %  FP  STO+ Z  R↓  ST* Y  LASTX  INT  X≠0?  GTO 02
31     R↓  R↓
33 END

Good code, I need to study how the NUMBER INVERSION procedure works; the percent % statement piques my curiosity.

On the other hand, I don't see anywhere where the control of the maximum number of loops or the detection of the maximum capacity of ten digits is done?!?

My code is 10 bytes longer, but it does all the due checks and it returns Y:0 X:0 on overflow digit capacity or loop number.
Code:
001 STO 01  SF 00  51 STO 02  0 STO 03    @ R01:actual integer   R02:cycles limit   R03:addition's counter                           
007 LBL 01                                                       @   MAIN LOOP
008   0  RCL 01
010   LBL 02  STO Z  10  ST/ T  ST* Z  MOD  +  X<>Y  INT  x>0?  GTO 02  @ REVERSE R01 digit by digit
021   10^X  10^X  X<>Y  RCL 01  FC?C 00  X≠Y?  ISG 03  GTO 03           @ If palindrome then exit but not first time :)
029   +  STO 01 (VIEW 01)                                               @ Update R01 ← R01+Pal(R01) 
031   LOG  x>Y?  CLRG                                                   @ Test for overflow ten digits
034 DSE 02  GTO 01                                                      @ Countdown and loop
036 CLRG                                                                @ CLEAR all REGISTERS to zero R01 and R03
037 LBL 03
038 RCL 03  RCL 01 (TONE 5)                                      @ END with Y:nb cycle  and X:final palindrome

   


Well, I haven't found any integers that approach the 50 iterations limit without exceeding the 10 digits limit!
Besides, I had a hard time finding an integer that don't give a palindrome in a few turns of the wheel!

Sadly, the main part of all the integers I try have a tendency to rapidly form a palindrome.
At least 196 works!

P.S.1:
Attention, in case of overflow, my code resets all the registers to zero. I'm a lazy person who doesn't try to preserve anything and finds it fine to use CLRGs shorter than 0 STO 03 STO 01.

P.S.2:
The printed version contains on step 31 a VIEW 01 to see what is happening and on step 41 a TONE 5 to sound the end of the race. These two statements are optional. If I could have been present at HHC 2022, they would not have appeared on the copy I would have given to the jury.

Thanks for sharing this. I had a lot of fun developing my code on my old, robust and reliable HP-41C
Find all posts by this user
Quote this message in a reply
09-12-2022, 09:01 PM (This post was last modified: 09-12-2022 09:02 PM by Craig Bladow.)
Post: #26
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Here is my entry that won the RPN part of the HHC 2022 programming contest this year. It is 76 bytes long. CLS is the raw file decoder's output for the command that clears the statistics registers. Likewise '#' is not equals.
Code:

LBL "PAL2"
STO 00
.05
STO 01
LBL 01
ISG 01
GTO 04
GTO 08
LBL 04
RCL 00
10
10^X
X<=Y?
GTO 08
X<>Y
XEQ 07
ST+ 00
RCL 00
XEQ 07
RCL 00
X=Y?
GTO 09
GTO 01
LBL 07
CLS
STO 11
LBL 06
10
ST* 12
MOD
ST- 11
ST+ 12
LASTX
ST/ 11
RCL 11
X#0?
GTO 06
RCL 12
RTN
LBL 08
CLST
STOP
LBL 09
RCL 01
INT
X<>Y
END

Try CC41!
Find all posts by this user
Quote this message in a reply
09-13-2022, 07:46 AM
Post: #27
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-12-2022 08:30 PM)C.Ret Wrote:  On the other hand, I don't see anywhere where the control of the maximum number of loops or the detection of the maximum capacity of ten digits is done?!?

I missed that in the problem description..

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
09-13-2022, 11:28 AM (This post was last modified: 09-15-2022 10:50 PM by Gene.)
Post: #28
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
FYI - the intent was to not use sysrpl and not use any sysevals or flashevals.

The "feel" of instructions in a forum is different than in a room with others. :-)

Input numbers used to test were:

5 - 11 and 2

88 - 44044 and 6

188 - 233332 and 7

98 - 0,0 on the RPN contest and RPL is 0,0. (answer is 8,813,200,023,188 and 24)

99899 - 2,217,777,122 and 10

9338 - 0 and 0 on RPN. on RPL 133,697,796,331 and 15.
Find all posts by this user
Quote this message in a reply
09-13-2022, 01:23 PM
Post: #29
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Here's a version that checks only the precision, in a machine-independent way, so the same code will run on a 41, 42 and Free42, making max. use of the available precision.
(eg. 196 will run out of precision on Free42 after 71 iterations.)

Code:
00 { 65-Byte Prgm }
01▸LBL "HHC2022"
02 STO 00
03 STO- 00
04 XEQ 10
05▸LBL 03 @ main loop
06 RCL ST Y
07 RCL ST Y
08 +
09 -
10 +/-
11 X≠Y?
12 CLST
13 X=0?
14 RTN
15 LASTX
16 XEQ 10
17 ISG 00
18 X<>Y
19 X≠Y?
20 GTO 03
21 RCL 00 @ end
22 X<>Y
23 RTN
24▸LBL 10 @ reverse number X -> revX X
25 ENTER
26 ENTER
27 -
28 10
29 LASTX
30▸LBL 02 @ X 10 Xrev Xorig
31 %
32 FP
33 STO+ ST Z
34 R↓
35 STO× ST Y
36 LASTX
37 IP
38 X≠0?
39 GTO 02
40 R↓
41 R↓
42 END

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
09-13-2022, 06:21 PM (This post was last modified: 09-15-2022 12:07 PM by Gerson W. Barbosa.)
Post: #30
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-12-2022 08:03 PM)Gerson W. Barbosa Wrote:  (# 559Ah, 221 bytes).

Code:
 « R→I -105 CF 0 SWAP
  « →STR { "0" } 1 PICK3 SIZE
    START OVER HEAD + SWAP TAIL SWAP
    NEXT NIP "0" OVER TAIL REVLIST +
  » → h
  «
    DO h EVAL ∑LIST OBJ→ SWAP ∑LIST OBJ→ + SWAP 1 + SWAP DUP h EVAL
    UNTIL == PICK3 50 ≥ OR
    END OVER 50 == { DROP2 0 DUP } IFT
  »
»

We can save a few bytes by eliminating the local variable:

Code:


« R→I -105 CF
  « →STR { "0" } 1 PICK3 SIZE
    START OVER HEAD + SWAP TAIL SWAP
    NEXT NIP "0" OVER TAIL REVLIST +
  » 0 ROT
  DO PICK3 EVAL ∑LIST OBJ→ SWAP ∑LIST OBJ→ + SWAP 1 + SWAP DUP 4 PICK EVAL
  UNTIL == PICK3 50 ≥ OR
  END ROT DROP OVER 50 == { DROP2 0 DUP } IFT
»

# 1924h, 212.5

P.S.: Or # FD3Hh and 210 bytes, if I place the subprogram between { } instead of « » (I keep on forgetting that).
Find all posts by this user
Quote this message in a reply
09-14-2022, 01:00 AM (This post was last modified: 10-04-2022 05:44 PM by David Hayden.)
Post: #31
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Here is my winning RPL entry. It can definitely be improved but I ran out of time. It could be restructured so it only reverses the number once. That would mean the list that reverses could be inline.

I'll try to clean it up and post a followup to show what I'm talking about.

185 bytes. #31E0h
Code:
«  
   {                       @ Function to reverse digits
     0. SWAP
     WHILE
          DUP
     REPEAT                @ reversedNum oldNum
           10. IDIV2       @ IDIV2 gives quotient and remainder
           ROT
           10. * +
           SWAP
     END
     DROP
   }
   
   0. ROT
   DO
       @ Stack is L1:counter L2:num
       SWAP 1. + SWAP      @ increment counter
       DUP
       4. PICK EVAL        @ reverse the digits
       +                   @ num + reverse(num)
   UNTIL
       @ Adjust results if resultŠ1E12 or counter>50
       DUP 1E12 ≥          @ result ≥ 1E12
       PICK3 50. >          @ counter > 50
       OR
       IF
       THEN
           DROP2
           0. DUP          @ So the UNTIL ends with the right stuff
       END
       DUPDUP              
       5. PICK EVAL        @ reverse number
       ==                  @ Break out of loop if num == reverse(num)
   END
   ROT DROP
»
Find all posts by this user
Quote this message in a reply
09-14-2022, 12:47 PM
Post: #32
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-14-2022 01:00 AM)David Hayden Wrote:  Here is my winning RPL entry. It can definitely be improved but I ran out of time. It could be restructured so it only reverses the number once. That would mean the list that reverses could be inline.

I'll try to clean it up and post a followup to show what I'm talking about.

I look forward to your revisions, David! We had a similar approach, so of course I think yours is great. Smile
Code:
@ Size: 180.0 bytes, CRC: 31DCh

\<<
  @ reversing subroutine
  \<<
    ""                        @ initial result is empty
    WHILE
      OVER                    @ continue while given quotient > 0
    REPEAT
      SWAP 10 IDIV2           @ pop the last digit from current value
      ROT SWAP +              @ append the digit to the result
    END
    NIP STR\->                @ drop final quotient, convert string to result
  \>> \-> r

  \<<
    0 SWAP                    @ loop counter
    DO
      DUP r EVAL              @ determine increment
      +                       @ add increment to current value
      SWAP 1 + SWAP           @ increment loop counter
    UNTIL
      OVER 50 >               @ >50 attempts?
      OVER 1E12 \>= OR        @ value out of range?
      { DROP2 0 DUP 1 }       @ zero out result and abort
      { DUPDUP r EVAL == }    @ otherwise check for desired result
      IFTE
    END
  \>>
\>>

I can't recall if the 49 supports IDIV2, so this may be limited to the 49g+/50g. Additionally, this version gains 8 bytes if you happen to transfer it to a calculator set to approximate mode due to the size differences in the numeric constants.

@Gene: thanks for posting, and especially for putting up with the requests for clarification. While they may be a pain, it's important to know what is and isn't allowed. This is especially true when a few bytes/nibbles here and there can make all the difference.
Find all posts by this user
Quote this message in a reply
09-14-2022, 02:52 PM
Post: #33
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-14-2022 12:47 PM)DavidM Wrote:  I can't recall if the 49 supports IDIV2, so this may be limited to the 49g+/50g.

From a User RPL perspective, the 49G supports everything the 49g+/50g supports (of course excluding differences in behavior due to the smaller screen). This is because they use the same ROM -- I believe I have ROM 2.10 on one of my 49G calculators, for example.
Visit this user's website Find all posts by this user
Quote this message in a reply
09-14-2022, 06:08 PM
Post: #34
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-11-2022 01:30 AM)Gene Wrote:  Problem Description: If you add any positive integer to the integer formed by reversing its digits, then repeat the process with the resulting sum, and keep repeating this loop... will you eventually reach a sum that is palindromic?

Has this question been answered?

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
09-14-2022, 06:41 PM
Post: #35
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-14-2022 06:08 PM)Jeff O. Wrote:  Has this question been answered?

No.
Find all posts by this user
Quote this message in a reply
09-15-2022, 01:07 PM
Post: #36
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-14-2022 12:47 PM)DavidM Wrote:  
Code:
@ Size: 180.0 bytes, CRC: 31DCh

\<<
  @ reversing subroutine
  \<<
    ""                        @ initial result is empty
    WHILE
      OVER                    @ continue while given quotient > 0
    REPEAT
      SWAP 10 IDIV2           @ pop the last digit from current value
      ROT SWAP +              @ append the digit to the result
    END
    NIP STR\->                @ drop final quotient, convert string to result
  \>> \-> r

  \<<
    0 SWAP                    @ loop counter
    DO
      DUP r EVAL              @ determine increment
      +                       @ add increment to current value
      SWAP 1 + SWAP           @ increment loop counter
    UNTIL
      OVER 50 >               @ >50 attempts?
      OVER 1E12 \>= OR        @ value out of range?
      { DROP2 0 DUP 1 }       @ zero out result and abort
      { DUPDUP r EVAL == }    @ otherwise check for desired result
      IFTE
    END
  \>>
\>>

Here’s a version of your program with the subroutine on the stack, like David Hayden’s above:

Code:

@ # 92C9h, 171.5 bytes

« { ""
  WHILE OVER
  REPEAT SWAP 10 IDIV2 ROT SWAP +
  END NIP STR→ } 0 ROT
  DO DUP 4 PICK EVAL + SWAP 1 + SWAP
  UNTIL OVER 50 > OVER 1.E12 ≥ OR { DROP2 0 DUP 1 } { DUPDUP 5 PICK EVAL == } IFTE
  END ROT DROP
»
Find all posts by this user
Quote this message in a reply
09-15-2022, 01:33 PM
Post: #37
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-14-2022 06:41 PM)John Keith Wrote:  
(09-14-2022 06:08 PM)Jeff O. Wrote:  Has this question been answered?

No.

Good question, Wikipedia has an entry for Lycrel numbers .

While testing my contest entry algorithm I found that 196 did not produce a palindrome before the contest limits were exceed. According to the Wikipedia article this is the first Lycrel number.

Try CC41!
Find all posts by this user
Quote this message in a reply
09-15-2022, 02:17 PM
Post: #38
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-15-2022 01:33 PM)Craig Bladow Wrote:  
(09-14-2022 06:41 PM)John Keith Wrote:  No.

Good question, Wikipedia has an entry for Lycrel numbers .

I was about to post with what I found via the internet-search-engine search that I should have done prior to my initial inquiry, but Craig beat me to it while I was preparing my response. Since I found some info, I'll go ahead and provide:

Quote:A Lychrel number is a natural number that cannot form a palindrome through the iterative process of repeatedly reversing its digits and adding the resulting numbers.

It is conjectured that 196 and other numbers that have not yet yielded a palindrome are Lychrel numbers, but no number in base ten has yet been proven to be Lychrel. Numbers which have not been demonstrated to be non-Lychrel are informally called "candidate Lychrel" numbers.

As Craig noted, 196 is the smallest candidate Lychrel number, having been reversed and added to create a number with a billion digits with no palindrome yet found.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
09-15-2022, 02:22 PM
Post: #39
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
Has anyone determined if the check for iterations>50 is necessary? In other words, are there any numbers that hit 50 iterations before overflowing 10 or 12 digits?
Find all posts by this user
Quote this message in a reply
09-15-2022, 03:01 PM
Post: #40
RE: HHC 2022 - Programming Contest - no responses until after 6am Monday 6am CENTRAL
(09-15-2022 02:22 PM)David Hayden Wrote:  Has anyone determined if the check for iterations>50 is necessary? In other words, are there any numbers that hit 50 iterations before overflowing 10 or 12 digits?

Adding a number to its reverse doubles the number, 'on average' ;-)
2^50 > 1e15, so I guess not.

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
Post Reply 




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