HP Forums
Programming Challenge: Palindromic Numbers on HP 50g - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Programming Challenge: Palindromic Numbers on HP 50g (/thread-9922.html)

Pages: 1 2


Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-13-2018 10:23 AM

I just published 2 programmes for producing palindromic numbers

http://www.hpmuseum.org/forum/thread-9921.html

& they work very nicely. They may be of use in this challenge (or not).

The problem now is how to interleave the 2 sequences so as to produce

https://oeis.org/A002113

ie all the palindromic numbers in their natural order as in the OEIS reference.

I would like a UserRPL programme that for input, say 98765432167 returns the 98765432167th member of the series.

Special merit given for short programmes & even more so for speedy ones.


RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-13-2018 04:33 PM

Code:
<< DUP 10
  IF <=
  THEN 1 -
  ELSE DUP DUP 2 / LOG IP ALOG SWAP OVER 11 *
    IF >=
    THEN 10 * SWAP OVER - SWAP OVER * SWAP REV +
    ELSE SWAP OVER - DUP 10 / IP REV 3 ROLLD * +
    END
  END
>>

'PALIN' STO

<< 0
  DO 10 * OVER 10 MOD + SWAP 10 / IP SWAP
  UNTIL OVER 0 ==
  END SWAP DROP
>>

'REV' STO

1 PALIN => 0
2 PALIN => 1
10 PALIN => 9
11 PALIN => 11
etc.

This uses standard math so it can only generate palindromes up to 12 digits, but I imagine it shouldn't be hard to modify for extended-precision math.


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gilles59 - 01-13-2018 05:09 PM

This works both in UserRPL (exact mode) and NewRPL (1E31 SETNFMT to get all the signifiant digits) . Naive approach

Code:
«
  0  -> Nmax N
  «
    -1 
    DO
     1 +
     IF DUP ->STR DUP SREV == THEN 1 'N' STO+ END
    UNTIL Nmax N == END
  »
»

In NewRPL with the simulator, you can see that :

1000 Palin -> 90009 (0.081 sec on the simulator, 4.5s on the NewRPL-HP50g, UserRPL : 76 sec with Emu48 FastMode, sloooow with a stock HP50 )
2000 Palin -> 1000001
3000 Palin -> 2000002
etc.
5000 Palin -> 4000004 (Simulator : 3,3 sec - NewRP HP50g : 190sec )

So we can imagine a way for a less naive approach for big numbers ;D


RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-13-2018 06:48 PM

HP-42S version of my program:

Code:
00 { 85-Byte Prgm }
01▸LBL "PALIN"
02 10
03 X<>Y
04 X>Y?
05 GTO 00
06 1
07 -
08 RTN
09▸LBL 00
10 RCL ST X
11 2
12 ÷
13 LOG
14 IP
15 10↑X
16 X<>Y
17 11
18 RCL× ST Z
19 X≤Y?
20 GTO 01
21 R↓
22 RCL- ST Y
23 ×
24 STO 00
25 LASTX
26 10
27 ÷
28 IP
29 XEQ "REV"
30 RCL+ 00
31 RTN
32▸LBL 01
33 R↓
34 X<>Y
35 10
36 ×
37 X<>Y
38 RCL- ST Y
39 X<>Y
40 RCL× ST Y
41 STO 00
42 R↓
43 XEQ "REV"
44 RCL+ 00
45 END

00 { 29-Byte Prgm }
01▸LBL "REV"
02 0
03 X<>Y
04▸LBL 00
05 RCL ST X
06 10
07 STO× ST T
08 STO÷ ST Z
09 MOD
10 STO+ ST Z
11 R↓
12 IP
13 X≠0?
14 GTO 00
15 R↓
16 END

5000 XEQ "PALIN" => 4000004 in 1.23 seconds (HP-42S).

Of course it's faster in Free42 or in a DM42, and then you can get palindromes up to 34 digits.

98765432167 XEQ "PALIN" => 887654321676123456788 in 0.01 seconds (DM42).


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-13-2018 09:53 PM

Very nice, Thomas, & here a slightly altered version which will deal with the entry 98765432167 & return the correct answer!

By the way, SAME is much faster than ==.

NTOK

Code:
« DUP 10 <=
  IF
  THEN 1 -
  ELSE DUPDUP 2
IQUOT SIZE R→I 1 -
10 SWAP ^ SWAP OVER
11 * >=
    IF
    THEN 10 * SWAP
OVER - SWAP OVER *
SWAP NREV +
    ELSE SWAP OVER
- DUP 10 IQUOT NREV
UNROT * +
    END
  END
»

NREV

Code:
« 0
  DO 10 * OVER 10
MOD + SWAP 10 IQUOT
SWAP OVER 0 SAME
  UNTIL
  END NIP
»



RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-13-2018 11:21 PM

(01-13-2018 09:53 PM)Gerald H Wrote:  Very nice, Thomas, & here a slightly altered version which will deal with the entry 98765432167 & return the correct answer!

By the way, SAME is much faster than ==.

Thanks!

My code is a bit limited by the fact that I do these RPL challenges in m48 on my iPhone, and that means I'm effectively using a 48GX. I see that m48+ emulates the 49G+ as well; is that equivalent to a 50g, software-wise, or should I look specifically for a 50g emulator?


RE: Programming Challenge: Palindromic Numbers on HP 50g - rprosperi - 01-13-2018 11:32 PM

(01-13-2018 11:21 PM)Thomas Okken Wrote:  I see that m48+ emulates the 49G+ as well; is that equivalent to a 50g, software-wise, or should I look specifically for a 50g emulator?

The 49g+ and 50g are functionally identical - the only differences were in the h/w. The latest f/w release is 2.15, so check if that's installed ("VERSION") and if not, I'd upgrade it, assuming the emulator allows that.


RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-14-2018 01:30 AM

(01-13-2018 11:32 PM)rprosperi Wrote:  
(01-13-2018 11:21 PM)Thomas Okken Wrote:  I see that m48+ emulates the 49G+ as well; is that equivalent to a 50g, software-wise, or should I look specifically for a 50g emulator?

The 49g+ and 50g are functionally identical - the only differences were in the h/w. The latest f/w release is 2.15, so check if that's installed ("VERSION") and if not, I'd upgrade it, assuming the emulator allows that.

I installed m48+, selected 49G+ emulation. VERSION reports 2.15.
Sweet, thanks! Good to go and explore something new.
(New to me, anyway. Smile )


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-14-2018 06:30 AM

Edit: Programme cleansed of inaccurate symbols.

A faster version, obviating REV

NNTOK

Code:
« DUP 10 ≤
  IF
  THEN 1 -
  ELSE DUPDUP 2
IQUOT SIZE R→I 1 -
10 SWAP ^ SWAP OVER
11 * ≥
    IF
    THEN 10 * SWAP
OVER - SWAP OVER *
SWAP →STR SREV OBJ→ +
    ELSE SWAP OVER
- DUP 10 IQUOT →STR SREV OBJ→
UNROT * +
    END
  END
»



RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-14-2018 01:11 PM

(01-14-2018 06:30 AM)Gerald H Wrote:  A faster version, obviating REV

NNTOK

[snip]

I tried running this in m48+ emulating the 49G+, and it doesn't recognize SREV.
The calculator has all the default settings; all I did before entering the program was switch from ALG to RPN mode.

I should obviously read the manual, and I will, but is there a quick fix for this problem?


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gilles59 - 01-14-2018 01:59 PM

(01-14-2018 01:11 PM)Thomas Okken Wrote:  I tried running this in m48+ emulating the 49G+, and it doesn't recognize SREV.
The calculator has all the default settings; all I did before entering the program was switch from ALG to RPN mode.

I should obviously read the manual, and I will, but is there a quick fix for this problem?

Hi, try

-86 SF

For 'infinite integer" RightShift & ENTER (toggle Real mode and exact mode)


RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-14-2018 02:06 PM

(01-14-2018 01:59 PM)Gilles59 Wrote:  -86 SF

That didn't work -- it's still not recognizing SREV.


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gilles59 - 01-14-2018 02:10 PM

(01-14-2018 02:06 PM)Thomas Okken Wrote:  
(01-14-2018 01:59 PM)Gilles59 Wrote:  -86 SF

That didn't work -- it's still not recognizing SREV.
Try ON & F3 (in the same time) like when you attch a lib

Extract of AUR :
Quote:The Development Library
----------------------------
Introduction
Built into the calculator is a set of functions not accessible to the user by default. These functions are in a library
that contains low level development tools mainly designed for use in developing System RPL and assembly
programs.
In order to enable this library, you must attach it with the command 256 ATTACH or by setting flag –86. When the
library is attached after the next warmstart (or reset), it appears in the APPS menu. You may reset the calculator by
pressing ON and F3 at the same time.



RE: Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-14-2018 02:17 PM

(01-14-2018 02:06 PM)Thomas Okken Wrote:  
(01-14-2018 01:59 PM)Gilles59 Wrote:  -86 SF

That didn't work -- it's still not recognizing SREV.

Gilles is correct, you must attach the built-in Development Lib, number 256.

The additional functions in lib 256 are a boon for anyone programming 49G to 50g.


RE: Programming Challenge: Palindromic Numbers on HP 50g - rprosperi - 01-14-2018 02:37 PM

(01-14-2018 02:17 PM)Gerald H Wrote:  
(01-14-2018 02:06 PM)Thomas Okken Wrote:  That didn't work -- it's still not recognizing SREV.

Gilles is correct, you must attach the built-in Development Lib, number 256.

The additional functions in lib 256 are a boon for anyone programming 49G to 50g.

@Thomas - after setting the flag (or attaching), press [APPS] to verify the "Development lib" is present as App #15 at the bottom. Selecting it should change the menu to [->H], [H->]. etc. SREV is on the 3rd page (press [NEXT] twice]).


RE: Programming Challenge: Palindromic Numbers on HP 50g - Thomas Okken - 01-14-2018 02:46 PM

Ah, so I was missing the ON+F3 step.

Did that, and now SREV works!

The program still doesn't work, with the same error message as before, which I'm guessing means that it needs to be re-parsed first. Next challenge: find the EDIT command. Boy, this keyboard layout really looks nothing like the 48G. Smile I really have to start reading the manual, I don't want to spam this thread with 49G+ rookie questions.

Thank you Gerald, Gilles, and Bob, for helping me get started!


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gilles59 - 01-14-2018 03:51 PM

(01-14-2018 02:46 PM)Thomas Okken Wrote:  ...find the EDIT command

To edit a program I press :

' (the O key)
The key of the soft menu of the program i want to edit
ENTER
LeftShift & DownKey


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-14-2018 04:05 PM

If you enter RCLF you will recall your flag settings.

My flag settings are

{ # 3800030205010FF0h # 8000000000000000h # 1000040A000008h # 0h }

If you enter this list & then STOF you will have the same flag settings as me.


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gilles59 - 01-14-2018 04:10 PM

(01-14-2018 06:30 AM)Gerald H Wrote:  A faster version, obviating REV

NNTOK

Code:
« DUP 10 <=
  IF
  THEN 1 -
  ELSE DUPDUP 2
IQUOT SIZE R→I 1 -
10 SWAP ^ SWAP OVER
11 * >=
    IF
    THEN 10 * SWAP
OVER - SWAP OVER *
SWAP →STR SREV OBJ→ +
    ELSE SWAP OVER
- DUP 10 IQUOT →STR SREV OBJ→
UNROT * +
    END
  END
»

Beautiful ! Works fine for me (in exact mode and with correct <= and >= characters)/ Now i have do understand_why_ this works


RE: Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-14-2018 04:10 PM

The <= in the programme as depicted in the thread does not exist on 50g - correct form is



Similarly >= is