HP Forums
HP 50g: Programming Problem: Integer Partition in Palindromic Integers - 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: HP 50g: Programming Problem: Integer Partition in Palindromic Integers (/thread-10026.html)

Pages: 1 2


RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers - Gerald H - 02-13-2018 09:31 PM

Meanwhile here's a shorter version of the programme:

Size: 261.5 Bytes

CkSum: # E377h

Code:
« 0 SWAP
  DO SWAP 1 + SWAP DUPDUP →STR DUP SREV DUP2 SAME
    IF
    THEN DROP2
    ELSE OBJ→ 1 SAME
      IF
      THEN DROP2 1 - SWAP 1 + 1 1
      ELSE 1. OVER SIZE 2. / DUP IP 5. ROLLD SUB DUPDUP 1. 6. PICK SUB SREV + OBJ→ ROT OVER <
        IF
        THEN DROP OBJ→ 1 - →STR DUP 1. 4. PICK SUB SREV + OBJ→ DUP
        END UNROT DROP2
      END
    END DUP 4 ROLLD - DUP NOT
  UNTIL
  END DROP DUPDUP 2. + ROLLD →LIST SWAP →TAG
»



RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers - Juan14 - 02-18-2018 01:50 AM

Hello Gerald H, in my previous program I wanted to see if I can have an IFTE structure inside another one and it did work. The problem with comparing the two strings got me thinking that it is not necessary to compare the hole number but just last half of it. Now the program is 195 bytes and the check-sum is # 4007h. Thanks for the remarks that help improve the program.
Code:
«
 WHILE DUP →STR DUPDUP SIZE DUP 1 >
 REPEAT
  2 / SWAP 1 PICK3 SUB DUP OBJ→ 1 - →STR
  IF DUP2 SIZE SWAP SIZE ==
  THEN
   4 ROLL 4 PICK 1 + OVER SIZE SUB OBJ→
   PICK3 1 6 PICK FLOOR SUB OBJ→ > UNROT IFTE
   DUP 1 4 ROLL FLOOR SUB + OBJ→ SWAP OVER -
  ELSE
   4 DROPN 1 - 1
  END
 END 3 DROPN
»



RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers - Gerald H - 02-18-2018 06:19 AM

I have found two numbers that cause problems for the programme.

5186197216518619721

causes the programme to go into an infinite loop &

5936136460563669585

returns

5936136459593613645
970049700
6161
77
2

three of which are not palindromes.


RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers - Gerald H - 02-19-2018 04:39 PM

Here is my version of Juan14's programme, now gives correct answers for any positive integer:

SIZE: 279.

CKSM: # 55A3h

Code:
« 0. SWAP
  WHILE SWAP 1. + SWAP DUP 9 >
  REPEAT DUP →STR DUP SREV OBJ→ 1 SAME
    IF
    THEN DROP 1 - SWAP 1
    ELSE DUP SIZE 2. / SWAP 1. PICK3 SUB DUPDUP 1. 5. PICK FLOOR SUB SREV + 4 PICK OVER OBJ→ <
      IF
      THEN DROP OBJ→ 1 - →STR DUP 1. 4. ROLL IP SUB SREV +
      ELSE UNROT DROP2
      END OBJ→ SWAP OVER - ROT SWAP
    END
  END DUP NOT
  IF
  THEN DROP 1. -
  ELSE SWAP
  END DUPDUP 2. + ROLLD →LIST SWAP R→I →TAG
»



RE: HP 50g: Programming Problem: Integer Partition in Palindromic Integers - Gerald H - 02-25-2018 03:41 PM

Good news!

Here is a proof that any integer can be partitioned in THREE palindromes:

https://arxiv.org/pdf/1602.06208v1.pdf

So the challenge is to produce a programme that returns such tripartite partitions.