Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions

m
(add FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
(21 intermediate revisions by 10 users not shown)
Line 8:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F reverse(=n, base)
V r = 0
L n > 0
Line 24:
print(‘#5’.format(i), end' " \n"[cnt % 12 == 0])
 
print()</langsyntaxhighlight>
 
{{out}}
Line 33:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC IsPalindrome(INT x BYTE base)
CHAR ARRAY digits="0123456789abcdef",s(16)
BYTE d,i,len
Line 64:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_in_base_10_that_are_palindromic_in_bases_2,_4,_and_16.png Screenshot from Atari 8-bit computer]
Line 72:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # show numbers in decimal that are palindromic in bases 2, 4 and 16 #
INT max number = 25 000; # maximum number to consider #
INT min base = 2; # smallest base needed #
Line 113:
END;
# print the numbers in decimal that are palendromic in bases 2, 4 and 16 #
# as noted by the REXX sample, even numbers ( other than 0 ) aren't #
FOR n FROM 0 TO max number DO
# applicable as even numbers end in 0 in base 2 so can't be palendromic #
print( ( " 0" ) ); # clearly, 0 is palendromic in all bases #
FOR n BY 2 TO max number DO
IF PALINDROMIC ( n DIGITS 16 ) THEN
IF PALINDROMIC ( n DIGITS 4 ) THEN
Line 122 ⟶ 125:
FI
OD
END</lang>
</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|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % find numbers palendromic in bases 2, 4, and 16 %
 
% returns true if n is palendromic in the specified base, false otherwide %
logical procedure palendromic( integer value n, base ) ;
begin
integer array digit( 1 :: 32 );
integer dPos, v, lPos, rPos;
logical isPalendromic;
dPos := 0;
v := n;
while v > 0 do begin
dPos := dPos + 1;
digit( dPos ) := v rem base;
v := v div base
end while_v_gt_0 ;
isPalendromic := true;
lPos := 1;
rPos := dPos;
while rPos > lPos and isPalendromic do begin
isPalendromic := digit( lPos ) = digit( rPos );
lPos := lPos + 1;
rPos := rPos - 1
end while_rPos_gt_lPos_and_isPalendromic ;
isPalendromic
end palendromic ;
% as noted by the REXX sample, all even numbers end in 0 in base 2 %
% so 0 is the only possible even number, note 0 is palendromic in all bases %
write( " 0" );
for n := 1 step 2 until 24999 do begin
if palendromic( n, 16 ) then begin
if palendromic( n, 4 ) then begin
if palendromic( n, 2 ) then begin
writeon( i_w := 1, s_w := 0, " ", n )
end if_palendromic__n_2
end if_palendromic__n_4
end if_palendromic__n_16
end for_n
end.
</syntaxhighlight>
{{out}}
<pre>
Line 130 ⟶ 180:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">(⊢(/⍨)(2 4 16∧.((⊢≡⌽)(⊥⍣¯1))⊢)¨)0,⍳24999</langsyntaxhighlight>
{{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|Arturo}}==
 
<syntaxhighlight lang="arturo">multiPalindromic?: function [n][
if (digits.base:2 n) <> reverse digits.base:2 n -> return false
if (digits.base:4 n) <> reverse digits.base:4 n -> return false
if (digits.base:16 n) <> reverse digits.base:16 n -> return false
return true
]
 
mpUpTo25K: select 0..25000 => multiPalindromic?
 
loop split.every: 12 mpUpTo25K 'x ->
print map x 's -> pad to :string s 5</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|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_IN_BASE_10_THAT_ARE_PALINDROMIC_IN_BASES_2_4_AND_16.AWK
# converted from C
Line 157 ⟶ 227:
return(r)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 167 ⟶ 237:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z: DEFDBL R
20 FOR I=1 TO 25000
30 B=2: GOSUB 100: IF R<>I GOTO 70
Line 179 ⟶ 249:
120 R=R*B+N MOD B
130 N=N\B
140 GOTO 110</langsyntaxhighlight>
{{out}}
<pre> 0 1 3 5 15
Line 188 ⟶ 258:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
manifest $( MAXIMUM = 25000 $)
 
Line 205 ⟶ 275:
for i = 0 to MAXIMUM
if palindrome(i,2) & palindrome(i,4) & palindrome(i,16)
do writef("%N*N", i)</langsyntaxhighlight>
{{out}}
<pre>0
Line 232 ⟶ 302:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#define MAXIMUM 25000
 
Line 258 ⟶ 328:
printf("\n");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
Line 264 ⟶ 334:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
Line 307 ⟶ 377:
ADD REV-DGT TO REVERSED
MOVE REV-NEXT TO REV-REST
GO TO REV-LOOP.</langsyntaxhighlight>
{{out}}
<pre> 0
Line 334 ⟶ 404:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 25000;
 
Line 363 ⟶ 433:
i := i + 1;
end loop;
print_nl();</langsyntaxhighlight>
{{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|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function GetRadixString(L: Integer; Radix: Byte): string;
{Converts integer a string of any radix}
const RadixChars: array[0..35] Of char =
('0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G','H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z');
var I: integer;
var S: string;
var Sign: string[1];
begin
Result:='';
If (L < 0) then
begin
Sign:='-';
L:=Abs(L);
end
else Sign:='';
S:='';
repeat
begin
I:=L mod Radix;
S:=RadixChars[I] + S;
L:=L div Radix;
end
until L = 0;
Result:=Sign + S;
end;
 
 
function IsPalindrome(N, Base: integer): boolean;
{Test if number is the same forward or backward}
{For a specific Radix}
var S1,S2: string;
begin
S1:=GetRadixString(N,Base);
S2:=ReverseString(S1);
Result:=S1=S2;
end;
 
function IsPalindrome2416(N: integer): boolean;
{Is N palindromic for bases 2, 4 and 16}
begin
Result:=IsPalindrome(N,2) and
IsPalindrome(N,4) and
IsPalindrome(N,16);
end;
 
procedure ShowPalindrome2416(Memo: TMemo);
{Show all numbers Palindromic for bases 2, 4 and 16}
var S: string;
var I,Cnt: integer;
begin
S:='';
Cnt:=0;
for I:=0 to 25000-1 do
if IsPalindrome2416(I) then
begin
Inc(Cnt);
S:=S+Format('%8D',[I]);
If (Cnt mod 5)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add('Count='+IntToStr(Cnt));
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Count=23
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}}==
'''begin'''
'''new''' palendromic; '''new''' n; '''label''' forN;
palendromic
&lt;- ` '''formal''' n; '''formal''' base;
'''begin'''
'''new''' v; '''new''' lPos; '''new''' rPos; '''new''' isPalendromic;
'''new''' digit;
'''label''' vGT0; '''label''' rGTl;
digit &lt;- '''list''' 64;
rPos &lt;- 0;
v &lt;- n;
vGT0: '''if''' v &gt; 0 '''then''' '''begin'''
rPos &lt;- rPos + 1;
digit[ rPos ] &lt;- v '''mod''' base;
v &lt;- v % base;
'''goto''' vGT0
'''end''' '''else''' 0;
isPalendromic &lt;- '''true''';
lPos &lt;- 1;
rGTl: '''if''' rPos &gt; lPos '''and''' isPalendromic '''then''' '''begin'''
isPalendromic &lt;- digit[ lPos ] = digit[ rPos ];
lPos &lt;- lPos + 1;
rPos &lt;- rPos - 1;
'''goto''' rGTl
'''end''' '''else''' 0;
isPalendromic
'''end'''
&apos;
;
'''out''' 0;
n &lt;- -1;
forN: '''if''' [ n &lt;- n + 2 ] &lt; 25000 '''then''' '''begin'''
'''if''' '''not''' palendromic( n, 16 ) '''then''' 0
'''else''' '''if''' '''not''' palendromic( n, 4 ) '''then''' 0
'''else''' '''if''' palendromic( n, 2 ) '''then''' '''out''' n
'''else''' 0
;
'''goto''' forN
'''end''' '''else''' 0
'''end''' $
{{out}}
<pre>
NUMBER 0
NUMBER 1
NUMBER 3
NUMBER 5
NUMBER 15
NUMBER 17
NUMBER 51
NUMBER 85
NUMBER 255
NUMBER 257
NUMBER 273
NUMBER 771
NUMBER 819
NUMBER 1285
NUMBER 1365
NUMBER 3855
NUMBER 4095
NUMBER 4097
NUMBER 4369
NUMBER 12291
NUMBER 13107
NUMBER 20485
NUMBER 21845
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Palindromic numbers in bases 2,4, and 16. Nigel Galloway: June 25th., 2021
let fG n g=let rec fG n g=[yield n%g; if n>=g then yield! fG(n/g) g] in let n=fG n g in n=List.rev n
Seq.initInfinite id|>Seq.takeWhile((>)25000)|>Seq.filter(fun g->fG g 16 && fG g 4 && fG g 2)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 381 ⟶ 610:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel math.parser prettyprint sequences ;
 
25,000 <iota> [
{ 2 4 16 } [ >base ] with map [ dup reverse = ] all?
] filter [ pprint bl ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 392 ⟶ 621:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function ispal( byval n as integer, b as integer ) as boolean
'determines if n is palindromic in base b
dim as string ns
Line 407 ⟶ 636:
for i as integer = 0 to 25000
if ispal(i,16) andalso ispal(i,4) andalso ispal(i,2) then print i;" ";
next i</langsyntaxhighlight>
{{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|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 450 ⟶ 679:
}
fmt.Println("\n\nFound", len(numbers), "such numbers.")
}</langsyntaxhighlight>
 
{{out}}
Line 460 ⟶ 689:
 
Found 23 such numbers.
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> palinbase=: (-: |.)@(#.inv)"0
I. (2&palinbase * 4&palinbase * 16&palinbase) i.25e3
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845
</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq and fq, the Go implementations'''
 
'''With minor tweaks, also works with jaq, the Rust implementation'''
 
This entry, which uses a stream-oriented approach to illustrate an
economical use of memory, uses `tobase` as found in the Wikipedia article on jq; it works for
bases up to 36 inclusive.
 
Use gojq or fq for unbounded-precision integer arithmetic.
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# nwise/2 assumes that null can be taken as the eos marker
def nwise(stream; $n):
foreach (stream, null) as $x ([];
if length == $n then [$x] else . + [$x] end;
if (.[-1] == null) and length>1 then .[:-1]
elif length == $n then .
else empty
end);
 
def tobase($b):
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1];
def mod: . % $b;
def div: ((. - mod) / $b);
def digits: recurse( select(. > 0) | div) | mod ;
# For jq it would be wise to protect against `infinite` as input, but using `isinfinite` confuses gojq
select( (tostring|test("^[0-9]+$")) and 2 <= $b and $b <= 36)
| if . == 0 then "0"
else [digits | digit] | reverse[1:] | add
end;
 
# boolean
def palindrome: explode as $in | ($in|reverse) == $in;
 
# boolean
def palindrome($b):
tobase($b) | palindrome;
def task($n):
"Numbers under \($n) in base 10 which are palindromic in bases 2, 4 and 16:",
(nwise(range(0;$n) | select(palindrome(2) and palindrome(4) and palindrome(16)); 5)
| map( lpad(6) ) | join(" "));
 
task(25000)
</syntaxhighlight>
{{output}}
<pre>
Numbers under 25000 in base 10 which are palindromic in bases 2, 4 and 16:
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|Julia}}==
<langsyntaxhighlight lang="julia">palinbases(n, bases = [2, 4, 16]) = all(b -> (d = digits(n, base = b); d == reverse(d)), bases)
 
foreach(p -> print(rpad(p[2], 7), p[1] % 11 == 0 ? "\n" : ""), enumerate(filter(palinbases, 1:25000)))
</langsyntaxhighlight>{{out}}
<pre>
1 3 5 15 17 51 85 255 257 273 771
819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
do -- find numbers palendromic in bases 2, 4, and 16
 
local function palendromic( n, base )
local digits, v = "", n
while v > 0 do
local dPos = ( v % base ) + 1
digits = digits..string.sub( "0123456789abcdef", dPos, dPos )
v = math.floor( v / base )
end
return digits == string.reverse( digits )
end
-- as noted by the REXX sample, all even numbers end in 0 in base 2
-- so 0 is the only possible even number, note 0 is palendromic in all bases
io.write( " 0" )
for n = 1, 24999, 2 do
if palendromic( n, 16 ) then
if palendromic( n, 4 ) then
if palendromic( n, 2 ) then
io.write( " ", n )
end
end
end
end
end
</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|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[PalindromeBaseQ, Palindrom2416Q]
PalindromeBaseQ[n_Integer, b_Integer] := PalindromeQ[IntegerDigits[n, b]]
Palindrom2416Q[n_Integer] := PalindromeBaseQ[n, 2] \[And] PalindromeBaseQ[n, 4] \[And] PalindromeBaseQ[n, 16]
Select[Range[0, 24999], Palindrom2416Q]
Length[%]</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}
23</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
type Digit = 0..15
Line 496 ⟶ 831:
 
echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 504 ⟶ 839:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'todigitstring';
Line 510 ⟶ 845:
sub pb { my $s = todigitstring(shift,shift); return $s eq join '', reverse split '', $s }
 
pb($_,2) and pb($_,4) and pb($_,16) and print "$_ " for 1..25000;</langsyntaxhighlight>
{{out}}
<pre>1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">palindrome</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 525 ⟶ 860:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">p2416</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 532 ⟶ 867:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 583 ⟶ 918:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
Line 589 ⟶ 924:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def reverse(n, base):
r = 0
while n > 0:
Line 605 ⟶ 940:
print("{:5}".format(i), end=" \n"[cnt % 12 == 0])
 
print()</langsyntaxhighlight>
{{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|Quackery}}==
 
<syntaxhighlight lang="Quackery">
[ temp put
0
[ over 0 > while
temp share tuck *
dip /mod +
again ]
temp release
nip ] is rev ( n n --> n )
 
[ dip dup rev = ] is pal ( n n --> b )
 
[]
25000 times
[ i^ 16 pal while
i^ 4 pal while
i^ 2 pal while
i^ join ]
echo</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|Raku}}==
<syntaxhighlight lang="raku" perl6line>put "{+$_} such numbers:\n", .batch(10)».fmt('%5d').join("\n") given
(^25000).grep: -> $n { all (2,4,16).map: { $n.base($_) eq $n.base($_).flip } }</langsyntaxhighlight>
{{out}}
<pre>23 such numbers:
Line 628 ⟶ 989:
This REXX version takes advantage that no &nbsp; ''even'' &nbsp; integers need be tested &nbsp; (except for the single exception: &nbsp; zero),
<br>this makes the execution twice as fast.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds non─neg integers that are palindromes in base 2, 4, and 16, where N<25k*/
numeric digits 100 /*ensure enough dec. digs for large #'s*/
parse arg n cols . /*obtain optional argument from the CL.*/
Line 665 ⟶ 1,026:
base: procedure; parse arg #,t,,y; @= 0123456789abcdefghijklmnopqrstuvwxyz /*up to 36*/
@@= substr(@, 2); do while #>=t; y= substr(@, #//t + 1, 1)y; #= # % t
end; return substr(@, #+1, 1)y</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 679 ⟶ 1,040:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 722 ⟶ 1,083:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 734 ⟶ 1,095:
Found 22 numbers
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
====Brute force====
« ""
OVER SIZE 1 '''FOR''' j
OVER j DUP SUB +
'''NEXT''' SWAP DROP
» '<span style="color:blue">REVSTR</span>' STO
« → base
« ""
'''WHILE''' OVER '''REPEAT'''
SWAP base MOD LASTARG / IP
"0123456789ABCDEF" ROT 1 + DUP SUB ROT +
'''END''' SWAP DROP
» » '<span style="color:blue">D→B</span>' STO
« '''CASE'''
HEX DUP R→B →STR 3 OVER SIZE SUB DUP <span style="color:blue">REVSTR</span> ≠ '''THEN''' DROP 0 '''END'''
DUP 4 <span style="color:blue">D→B</span> DUP <span style="color:blue">REVSTR</span> ≠ '''THEN''' DROP 0 '''END'''
BIN DUP R→B →STR 3 OVER SIZE SUB DUP <span style="color:blue">REVSTR</span> ==
'''END'''
» '<span style="color:blue">PAL2416</span>' STO
« { 0 }
1 25000 '''FOR''' n
'''IF''' n <span style="color:blue">PAL2416</span> '''THEN''' n + '''END'''
2 '''STEP'''
» '<span style="color:blue">TASK</span>' STO
Runs in 42 minutes on a HP-48SX.
 
====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
« HEX R→B →STR → h
« "#"
h SIZE 1 - 3 '''FOR''' j
h j DUP SUB +
-1 '''STEP'''
"h" + STR→ B→R
» » ‘<span style="color:blue">REVHEX</span>’ STO
« { }
0 15 '''FOR''' b
'''IF''' b <span style="color:blue">PAL24?</span> '''THEN''' b + '''END NEXT'''
1 2 '''FOR''' x
-1 15 '''FOR''' m
16 x 1 - ^ 16 x ^ 1 - '''FOR''' b
b
'''IF''' m 0 ≥ '''THEN''' 16 * m + '''END'''
16 x ^ *
b <span style="color:blue">REVHEX</span> +
'''IF''' DUP <span style="color:blue">PAL24?</span> '''THEN''' +
'''ELSE IF''' 25000 ≥ '''THEN''' KILL '''END''' <span style="color:grey">@ not idiomatic but useful to exit 3 nested loops</span>
'''END'''
'''NEXT NEXT NEXT'''
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}}
<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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">res = (0..25000).select do |n|
[2, 4, 16].all? do |base|
b = n.to_s(base)
b == b.reverse
end
end
puts res.join(" ")</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|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: palindrome (in string: input) is
Line 752 ⟶ 1,204:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 759 ⟶ 1,211:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say gather {
for (var k = 0; k < 25_000; k = k.next_palindrome(16)) {
take(k) if [2, 4].all{|b| k.is_palindrome(b) }
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 771 ⟶ 1,223:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Conv, Fmt
{{libheader|Wren-seq}}
<lang ecmascript>import "/fmt" for Conv, Fmt
import "/seq" for Lst
 
System.print("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
Line 787 ⟶ 1,237:
}
}
for (chunk in Lst.chunks(numbers, 8)) Fmt.printtprint("$,6d", chunknumbers, 8)
System.print("\nFound %(numbers.count) such numbers.")</langsyntaxhighlight>
 
{{out}}
Line 801 ⟶ 1,251:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Reverse(N, Base); \Reverse order of digits in N for given Base
int N, Base, M;
[M:= 0;
Line 824 ⟶ 1,274:
Text(0, " such numbers found.
");
]</langsyntaxhighlight>
 
{{out}}
9,476

edits