Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content deleted Content added
RPL header (was incorrectly "Ruby") |
→{{header|RPL}}: faster approach |
||
Line 943:
=={{header|RPL}}==
{{works with|HP|48}}
====Brute force====
« ""
OVER SIZE 1 '''FOR''' j
Line 968 ⟶ 969:
'''IF''' n <span style="color:blue">PAL2416</span> '''THEN''' n + '''END NEXT'''
» '<span style="color:blue">TASK</span>' STO
Runs in 1 hours 37 minutes on a HP-48SX.
{{out}}<pre>▼
====Much faster approach====
The task generates palindromes in base 16, which must then be verified as palindromes in the other two bases.
« 1 SF
R→B →STR 3 OVER SIZE 1 - SUB
0 1 '''FOR''' b
'''IF''' DUP SIZE b 1 + MOD '''THEN''' "0" SWAP + '''END'''
""
OVER SIZE b - 1 '''FOR''' j
OVER j DUP b + SUB +
-1 b - '''STEP'''
'''IF''' OVER ≠ '''THEN''' 1 CF 1 'b' STO '''END'''
'''NEXT''' DROP
1 FS?
» '<span style="color:blue">PAL24?</span>' STO
« BIN { 0 }
1 15 '''FOR''' j
'''IF''' j <span style="color:blue">PAL24?</span> '''THEN''' j + '''END'''
2 '''STEP'''
1 15 FOR j
j DUP 16 * +
'''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' + '''ELSE''' DROP '''END'''
2 '''STEP'''
1 15 '''FOR''' j
0 15 '''FOR''' k
j 256 * k 16 * + j +
'''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' + '''ELSE''' DROP '''END'''
'''NEXT''' 2 '''STEP'''
16 255 '''FOR''' j
'''IF''' j 16 MOD '''THEN'''
j 256 * j 16 MOD 16 * + j 16 / IP +
'''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' + '''ELSE IF''' 25000 > '''THEN''' 255 'j' STO '''END'''
'''END'''
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
Runs in 2 minutes 16 on a HP-48SX: 40 times faster than brute force!
<pre>
1: { 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 }
</pre>
|