Programming Challenge: Palindromic Numbers on HP 50g

+- HP Forums (http://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)



Programming Challenge: Palindromic Numbers on HP 50g - Gerald H - 01-13-2018 04: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 10:33 AM

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 11:09 AM

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 - Gerald H - 01-14-2018 10:41 AM

As the weekend draws to a close (here in Vienna) I post my attempt at answering the challenge.

The programme takes about half the time needed by NNTOK above.

The poetic name of the programme is

A002113.USR

Code:
« DUP 2. <
  IF
  THEN 0
  ELSE DUP →STR DUP
HEAD "1" SAME OVER
2. 2. SUB "0" SAME
NOT AND
    IF
    THEN TAIL DUP
SREV
    ELSE DUP TAIL
SWAP HEAD OBJ→ 1 -
DUP NOT
      IF
      THEN DROP
TAIL 9
      END
SWAP + DUP SREV
TAIL
    END + OBJ→
  END SWAP DROP
»