Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Numbers in base 10 that are palindromic in bases 2, 4, and 16 (view source)
Revision as of 20:35, 13 November 2023
, 8 months ago→Much faster approach: cleaned the code
(Added Euler) |
(→Much faster approach: cleaned the code) |
||
Line 1,130:
====Much faster approach====
The task generates palindromes in base 16, which must then be verified as palindromes in the other two bases.
« BIN 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
«
« "#"
1 15 '''FOR''' j▼
h
2 '''STEP'''▼
"h"
» »
« { }
▲ j 256 * k 16 * + j +
▲ '''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' + '''ELSE''' DROP '''END'''
16
16 x ^ *
'''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' + '''ELSE IF''' 25000 > '''THEN''' 255 'j' STO '''END''' ▼
b <span style="color:blue">REVHEX</span> +
▲ '''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' +
'''NEXT''' ▼
'''ELSE IF''' 25000 ≥ '''THEN''' KILL '''END''' <span style="color:grey">@ not idiomatic but useful to exit 3 nested loops</span>
SORT
» '<span style="color:blue">TASK</span>' STO
<span style="color:blue">TASK</span> SORT
Runs in 2 minutes 16 on a HP-48SX: 18 times faster than brute force!
{{out}}
|