Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Content deleted Content added
Added Euler |
|||
(6 intermediate revisions by 4 users not shown) | |||
Line 523:
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
func rev n base .
while n > 0
n = n div base
.
return r
.
for i = 0 to 25000 - 1
if rev i 2 = i and rev i 4 = i and rev i 16 = i
write i & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845
</pre>
=={{header|Euler}}==
Line 1,130 ⟶ 1,150:
====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
h j DUP SUB +
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}}
Line 1,213 ⟶ 1,239:
<pre>
[0, 1, 3, 5, 15, 17, 51, 85, 255, 257, 273, 771, 819, 1285, 1365, 3855, 4095, 4097, 4369, 12291, 13107, 20485, 21845]
</pre>
=={{header|Uiua}}==
▽⊸≡(/↧≡(≍⇌.⬚0↯⊟∞)[4 2 1]¤⋯)⇡25000
</syntaxhighlight>
{{out}}
<pre>
[0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845]
</pre>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv, Fmt
▲<syntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
System.print("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
Line 1,233 ⟶ 1,266:
}
}
System.print("\nFound %(numbers.count) such numbers.")</syntaxhighlight>
|