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

From Rosetta Code
Content added Content deleted
(J draft)
m (→‎{{header|Wren}}: Minor tidy)
 
(19 intermediate revisions by 9 users not shown)
Line 8: Line 8:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F reverse(=n, base)
<syntaxhighlight lang="11l">F reverse(=n, base)
V r = 0
V r = 0
L n > 0
L n > 0
Line 24: Line 24:
print(‘#5’.format(i), end' " \n"[cnt % 12 == 0])
print(‘#5’.format(i), end' " \n"[cnt % 12 == 0])


print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 33: Line 33:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC IsPalindrome(INT x BYTE base)
<syntaxhighlight lang="action!">BYTE FUNC IsPalindrome(INT x BYTE base)
CHAR ARRAY digits="0123456789abcdef",s(16)
CHAR ARRAY digits="0123456789abcdef",s(16)
BYTE d,i,len
BYTE d,i,len
Line 64: Line 64:
FI
FI
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{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]
[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: Line 72:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # show numbers in decimal that are palindromic in bases 2, 4 and 16 #
<syntaxhighlight 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 max number = 25 000; # maximum number to consider #
INT min base = 2; # smallest base needed #
INT min base = 2; # smallest base needed #
Line 113: Line 113:
END;
END;
# print the numbers in decimal that are palendromic in bases 2, 4 and 16 #
# 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 16 ) THEN
IF PALINDROMIC ( n DIGITS 4 ) THEN
IF PALINDROMIC ( n DIGITS 4 ) THEN
Line 122: Line 125:
FI
FI
OD
OD
END</lang>
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|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}}
{{out}}
<pre>
<pre>
Line 130: Line 180:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}
<lang apl>(⊢(/⍨)(2 4 16∧.((⊢≡⌽)(⊥⍣¯1))⊢)¨)0,⍳24999</lang>
<syntaxhighlight lang="apl">(⊢(/⍨)(2 4 16∧.((⊢≡⌽)(⊥⍣¯1))⊢)¨)0,⍳24999</syntaxhighlight>
{{out}}
{{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>
<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}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBERS_IN_BASE_10_THAT_ARE_PALINDROMIC_IN_BASES_2_4_AND_16.AWK
# syntax: GAWK -f NUMBERS_IN_BASE_10_THAT_ARE_PALINDROMIC_IN_BASES_2_4_AND_16.AWK
# converted from C
# converted from C
Line 157: Line 227:
return(r)
return(r)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 167: Line 237:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang basic>10 DEFINT A-Z: DEFDBL R
<syntaxhighlight lang="basic">10 DEFINT A-Z: DEFDBL R
20 FOR I=1 TO 25000
20 FOR I=1 TO 25000
30 B=2: GOSUB 100: IF R<>I GOTO 70
30 B=2: GOSUB 100: IF R<>I GOTO 70
Line 179: Line 249:
120 R=R*B+N MOD B
120 R=R*B+N MOD B
130 N=N\B
130 N=N\B
140 GOTO 110</lang>
140 GOTO 110</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 3 5 15
<pre> 0 1 3 5 15
Line 188: Line 258:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( MAXIMUM = 25000 $)
manifest $( MAXIMUM = 25000 $)


Line 205: Line 275:
for i = 0 to MAXIMUM
for i = 0 to MAXIMUM
if palindrome(i,2) & palindrome(i,4) & palindrome(i,16)
if palindrome(i,2) & palindrome(i,4) & palindrome(i,16)
do writef("%N*N", i)</lang>
do writef("%N*N", i)</syntaxhighlight>
{{out}}
{{out}}
<pre>0
<pre>0
Line 232: Line 302:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#define MAXIMUM 25000
#define MAXIMUM 25000


Line 258: Line 328:
printf("\n");
printf("\n");
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
Line 264: Line 334:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
Line 307: Line 377:
ADD REV-DGT TO REVERSED
ADD REV-DGT TO REVERSED
MOVE REV-NEXT TO REV-REST
MOVE REV-NEXT TO REV-REST
GO TO REV-LOOP.</lang>
GO TO REV-LOOP.</syntaxhighlight>
{{out}}
{{out}}
<pre> 0
<pre> 0
Line 334: Line 404:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 25000;
const MAXIMUM := 25000;


Line 363: Line 433:
i := i + 1;
i := i + 1;
end loop;
end loop;
print_nl();</lang>
print_nl();</syntaxhighlight>
{{out}}
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
<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>
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#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Palindromic numbers in bases 2,4, and 16. Nigel Galloway: June 25th., 2021
// 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
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 ""
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}}
{{out}}
<pre>
<pre>
Line 381: Line 610:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: io kernel math.parser prettyprint sequences ;
<syntaxhighlight lang="factor">USING: io kernel math.parser prettyprint sequences ;


25,000 <iota> [
25,000 <iota> [
{ 2 4 16 } [ >base ] with map [ dup reverse = ] all?
{ 2 4 16 } [ >base ] with map [ dup reverse = ] all?
] filter [ pprint bl ] each nl</lang>
] filter [ pprint bl ] each nl</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 392: Line 621:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>function ispal( byval n as integer, b as integer ) as boolean
<syntaxhighlight lang="freebasic">function ispal( byval n as integer, b as integer ) as boolean
'determines if n is palindromic in base b
'determines if n is palindromic in base b
dim as string ns
dim as string ns
Line 407: Line 636:
for i as integer = 0 to 25000
for i as integer = 0 to 25000
if ispal(i,16) andalso ispal(i,4) andalso ispal(i,2) then print i;" ";
if ispal(i,16) andalso ispal(i,4) andalso ispal(i,2) then print i;" ";
next i</lang>
next 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>
{{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}}==
=={{header|Go}}==
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 450: Line 679:
}
}
fmt.Println("\n\nFound", len(numbers), "such numbers.")
fmt.Println("\n\nFound", len(numbers), "such numbers.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 463: Line 692:


=={{header|J}}==
=={{header|J}}==
<lang J> palinbase=: (-: |.)@(#.inv)"0
<syntaxhighlight lang="j"> palinbase=: (-: |.)@(#.inv)"0
I.2 palinbase i.25e3
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
0 1 3 5 7 9 15 17 21 27 31 33 45 51 63 65 73 85 93 99 107 119 127 129 153 165 189 195 219 231 255 257 273 297 313 325 341 365 381 387 403 427 443 455 471 495 511 513 561 585 633 645 693 717 765 771 819 843 891 903 951 975 1023 1025 1057 1105 1137 1161 1193 1241 1273 1285 1317 1365 1397 1421 1453 1501 1533 1539 1571 1619 1651 1675 1707 1755 1787 1799 1831 1879 1911 1935 1967 2015 2047 2049 2145 2193 2289 2313 2409 2457 2553 2565 2661 2709 2805 2829 2925 2973 3069 3075 3171 3219 3315 3339 3435 3483 3579 3591 3687 3735 3831 3855 3951 3999 4095 4097 4161 4257 4321 4369 4433 4529 4593 4617 4681 4777 4841 4889 4953 5049 5113 5125 5189 5285 5349 5397 5461 5557 5621 5645 5709 5805 5869 5917 5981 6077 6141 6147 6211 6307 6371 6419 6483 6579 6643 6667 6731 6827 6891 6939 7003 7099 7163 7175 7239 7335 7399 7447 7511 7607 7671 7695 7759 7855 7919 7967 8031 8127 8191 8193 8385 8481 8673 8721 8913 9009 9201 9225 9417 9513 9705 9753 9945 10041 10233 10245 10437 10533 10725 10773 10965 11061 11253 11277 11469 11565 11757 11805 11997 12093 12285 12291 12483 12579 12771 12819 13011 13107 13299 13323 13515 13611 13803 13851 14043 14139 14331 14343 14535 14631 14823 14871 15063 15159 15351 15375 15567 15663 15855 15903 16095 16191 16383 16385 16513 16705 16833 16929 17057 17249 17377 17425 17553 17745 17873 17969 18097 18289 18417 18441 18569 18761 18889 18985 19113 19305 19433 19481 19609 19801 19929 20025 20153 20345 20473 20485 20613 20805 20933 21029 21157 21349 21477 21525 21653 21845 21973 22069 22197 22389 22517 22541 22669 22861 22989 23085 23213 23405 23533 23581 23709 23901 24029 24125 24253 24445 24573 24579 24707 24899
</syntaxhighlight>
I.8 palinbase i.25e3

0 1 2 3 4 5 6 7 9 18 27 36 45 54 63 65 73 81 89 97 105 113 121 130 138 146 154 162 170 178 186 195 203 211 219 227 235 243 251 260 268 276 284 292 300 308 316 325 333 341 349 357 365 373 381 390 398 406 414 422 430 438 446 455 463 471 479 487 495 503 511 513 585 657 729 801 873 945 1017 1026 1098 1170 1242 1314 1386 1458 1530 1539 1611 1683 1755 1827 1899 1971 2043 2052 2124 2196 2268 2340 2412 2484 2556 2565 2637 2709 2781 2853 2925 2997 3069 3078 3150 3222 3294 3366 3438 3510 3582 3591 3663 3735 3807 3879 3951 4023 4095 4097 4161 4225 4289 4353 4417 4481 4545 4617 4681 4745 4809 4873 4937 5001 5065 5137 5201 5265 5329 5393 5457 5521 5585 5657 5721 5785 5849 5913 5977 6041 6105 6177 6241 6305 6369 6433 6497 6561 6625 6697 6761 6825 6889 6953 7017 7081 7145 7217 7281 7345 7409 7473 7537 7601 7665 7737 7801 7865 7929 7993 8057 8121 8185 8194 8258 8322 8386 8450 8514 8578 8642 8714 8778 8842 8906 8970 9034 9098 9162 9234 9298 9362 9426 9490 9554 9618 9682 9754 9818 9882 9946 10010 10074 10138 10202 10274 10338 10402 10466 10530 10594 10658 10722 10794 10858 10922 10986 11050 11114 11178 11242 11314 11378 11442 11506 11570 11634 11698 11762 11834 11898 11962 12026 12090 12154 12218 12282 12291 12355 12419 12483 12547 12611 12675 12739 12811 12875 12939 13003 13067 13131 13195 13259 13331 13395 13459 13523 13587 13651 13715 13779 13851 13915 13979 14043 14107 14171 14235 14299 14371 14435 14499 14563 14627 14691 14755 14819 14891 14955 15019 15083 15147 15211 15275 15339 15411 15475 15539 15603 15667 15731 15795 15859 15931 15995 16059 16123 16187 16251 16315 16379 16388 16452 16516 16580 16644 16708 16772 16836 16908 16972 17036 17100 17164 17228 17292 17356 17428 17492 17556 17620 17684 17748 17812 17876 17948 18012 18076 18140 18204 18268 18332 18396 18468 18532 18596 18660 18724 18788 18852 18916 18988 19052 19116 19180 19244 19308 19372 19436 19508 19572 19636 19700 19764 19828 19892 19956 20028 20092 20156 20220 20284 20348 20412 20476 20485 20549 20613 20677 20741 20805 20869 20933 21005 21069 21133 21197 21261 21325 21389 21453 21525 21589 21653 21717 21781 21845 21909 21973 22045 22109 22173 22237 22301 22365 22429 22493 22565 22629 22693 22757 22821 22885 22949 23013 23085 23149 23213 23277 23341 23405 23469 23533 23605 23669 23733 23797 23861 23925 23989 24053 24125 24189 24253 24317 24381 24445 24509 24573 24582 24646 24710 24774 24838 24902 24966
=={{header|jq}}==
I.16 palinbase i.25e3
{{works with|jq}}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 257 273 289 305 321 337 353 369 385 401 417 433 449 465 481 497 514 530 546 562 578 594 610 626 642 658 674 690 706 722 738 754 771 787 803 819 835 851 867 883 899 915 931 947 963 979 995 1011 1028 1044 1060 1076 1092 1108 1124 1140 1156 1172 1188 1204 1220 1236 1252 1268 1285 1301 1317 1333 1349 1365 1381 1397 1413 1429 1445 1461 1477 1493 1509 1525 1542 1558 1574 1590 1606 1622 1638 1654 1670 1686 1702 1718 1734 1750 1766 1782 1799 1815 1831 1847 1863 1879 1895 1911 1927 1943 1959 1975 1991 2007 2023 2039 2056 2072 2088 2104 2120 2136 2152 2168 2184 2200 2216 2232 2248 2264 2280 2296 2313 2329 2345 2361 2377 2393 2409 2425 2441 2457 2473 2489 2505 2521 2537 2553 2570 2586 2602 2618 2634 2650 2666 2682 2698 2714 2730 2746 2762 2778 2794 2810 2827 2843 2859 2875 2891 2907 2923 2939 2955 2971 2987 3003 3019 3035 3051 3067 3084 3100 3116 3132 3148 3164 3180 3196 3212 3228 3244 3260 3276 3292 3308 3324 3341 3357 3373 3389 3405 3421 3437 3453 3469 3485 3501 3517 3533 3549 3565 3581 3598 3614 3630 3646 3662 3678 3694 3710 3726 3742 3758 3774 3790 3806 3822 3838 3855 3871 3887 3903 3919 3935 3951 3967 3983 3999 4015 4031 4047 4063 4079 4095 4097 4369 4641 4913 5185 5457 5729 6001 6273 6545 6817 7089 7361 7633 7905 8177 8194 8466 8738 9010 9282 9554 9826 10098 10370 10642 10914 11186 11458 11730 12002 12274 12291 12563 12835 13107 13379 13651 13923 14195 14467 14739 15011 15283 15555 15827 16099 16371 16388 16660 16932 17204 17476 17748 18020 18292 18564 18836 19108 19380 19652 19924 20196 20468 20485 20757 21029 21301 21573 21845 22117 22389 22661 22933 23205 23477 23749 24021 24293 24565 24582 24854</lang>
'''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}}==
=={{header|Julia}}==
<lang julia>palinbases(n, bases = [2, 4, 16]) = all(b -> (d = digits(n, base = b); d == reverse(d)), bases)
<syntaxhighlight 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)))
foreach(p -> print(rpad(p[2], 7), p[1] % 11 == 0 ? "\n" : ""), enumerate(filter(palinbases, 1:25000)))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 3 5 15 17 51 85 255 257 273 771
1 3 5 15 17 51 85 255 257 273 771
819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845
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>
</pre>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[PalindromeBaseQ, Palindrom2416Q]
<syntaxhighlight lang="mathematica">ClearAll[PalindromeBaseQ, Palindrom2416Q]
PalindromeBaseQ[n_Integer, b_Integer] := PalindromeQ[IntegerDigits[n, b]]
PalindromeBaseQ[n_Integer, b_Integer] := PalindromeQ[IntegerDigits[n, b]]
Palindrom2416Q[n_Integer] := PalindromeBaseQ[n, 2] \[And] PalindromeBaseQ[n, 4] \[And] PalindromeBaseQ[n, 16]
Palindrom2416Q[n_Integer] := PalindromeBaseQ[n, 2] \[And] PalindromeBaseQ[n, 4] \[And] PalindromeBaseQ[n, 16]
Select[Range[0, 24999], Palindrom2416Q]
Select[Range[0, 24999], Palindrom2416Q]
Length[%]</lang>
Length[%]</syntaxhighlight>
{{out}}
{{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>{0, 1, 3, 5, 15, 17, 51, 85, 255, 257, 273, 771, 819, 1285, 1365, 3855, 4095, 4097, 4369, 12291, 13107, 20485, 21845}
Line 492: Line 808:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils, sugar
<syntaxhighlight lang="nim">import strutils, sugar


type Digit = 0..15
type Digit = 0..15
Line 515: Line 831:


echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")</lang>
echo list.join(" ")</syntaxhighlight>


{{out}}
{{out}}
Line 523: Line 839:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory 'todigitstring';
use ntheory 'todigitstring';
Line 529: Line 845:
sub pb { my $s = todigitstring(shift,shift); return $s eq join '', reverse split '', $s }
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;</lang>
pb($_,2) and pb($_,4) and pb($_,16) and print "$_ " for 1..25000;</syntaxhighlight>
{{out}}
{{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>
<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}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 544: Line 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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 551: Line 867:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H:
<syntaxhighlight lang="plm">100H:
/* CP/M CALLS */
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 602: Line 918:
END;
END;
CALL EXIT;
CALL EXIT;
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
Line 608: Line 924:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def reverse(n, base):
<syntaxhighlight lang="python">def reverse(n, base):
r = 0
r = 0
while n > 0:
while n > 0:
Line 624: Line 940:
print("{:5}".format(i), end=" \n"[cnt % 12 == 0])
print("{:5}".format(i), end=" \n"[cnt % 12 == 0])


print()</lang>
print()</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
<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>
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}}==
=={{header|Raku}}==
<lang perl6>put "{+$_} such numbers:\n", .batch(10)».fmt('%5d').join("\n") given
<syntaxhighlight lang="raku" line>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 } }</lang>
(^25000).grep: -> $n { all (2,4,16).map: { $n.base($_) eq $n.base($_).flip } }</syntaxhighlight>
{{out}}
{{out}}
<pre>23 such numbers:
<pre>23 such numbers:
Line 647: Line 989:
This REXX version takes advantage that no &nbsp; ''even'' &nbsp; integers need be tested &nbsp; (except for the single exception: &nbsp; zero),
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.
<br>this makes the execution twice as fast.
<lang rexx>/*REXX pgm finds non─neg integers that are palindromes in base 2, 4, and 16, where N<25k*/
<syntaxhighlight 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*/
numeric digits 100 /*ensure enough dec. digs for large #'s*/
parse arg n cols . /*obtain optional argument from the CL.*/
parse arg n cols . /*obtain optional argument from the CL.*/
Line 684: Line 1,026:
base: procedure; parse arg #,t,,y; @= 0123456789abcdefghijklmnopqrstuvwxyz /*up to 36*/
base: procedure; parse arg #,t,,y; @= 0123456789abcdefghijklmnopqrstuvwxyz /*up to 36*/
@@= substr(@, 2); do while #>=t; y= substr(@, #//t + 1, 1)y; #= # % t
@@= substr(@, 2); do while #>=t; y= substr(@, #//t + 1, 1)y; #= # % t
end; return substr(@, #+1, 1)y</lang>
end; return substr(@, #+1, 1)y</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 698: Line 1,040:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
Line 741: Line 1,083:
binList = substr(binList,nl,"")
binList = substr(binList,nl,"")
return binList
return binList
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 753: Line 1,095:
Found 22 numbers
Found 22 numbers
done...
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>
</pre>


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func boolean: palindrome (in string: input) is
const func boolean: palindrome (in string: input) is
Line 771: Line 1,204:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 778: Line 1,211:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>say gather {
<syntaxhighlight lang="ruby">say gather {
for (var k = 0; k < 25_000; k = k.next_palindrome(16)) {
for (var k = 0; k < 25_000; k = k.next_palindrome(16)) {
take(k) if [2, 4].all{|b| k.is_palindrome(b) }
take(k) if [2, 4].all{|b| k.is_palindrome(b) }
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 790: Line 1,223:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{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:")
System.print("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
Line 806: Line 1,237:
}
}
}
}
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$,6d", chunk)
Fmt.tprint("$,6d", numbers, 8)
System.print("\nFound %(numbers.count) such numbers.")</lang>
System.print("\nFound %(numbers.count) such numbers.")</syntaxhighlight>


{{out}}
{{out}}
Line 820: Line 1,251:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func Reverse(N, Base); \Reverse order of digits in N for given Base
<syntaxhighlight lang="xpl0">func Reverse(N, Base); \Reverse order of digits in N for given Base
int N, Base, M;
int N, Base, M;
[M:= 0;
[M:= 0;
Line 843: Line 1,274:
Text(0, " such numbers found.
Text(0, " such numbers found.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}

Latest revision as of 11:38, 6 January 2024

Numbers in base 10 that are palindromic in bases 2, 4, and 16 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Find numbers in base 10 that are palindromic in bases 2, 4, and 16, where n < 25,000



11l

Translation of: Python
F reverse(=n, base)
   V r = 0
   L n > 0
      r = r * base + n % base
      n I/= base
   R r

F palindrome(n, base)
   R n == reverse(n, base)

V cnt = 0
L(i) 25000
   I all((2, 4, 16).map(base -> palindrome(@i, base)))
      cnt++
      print(‘#5’.format(i), end' " \n"[cnt % 12 == 0])

print()
Output:
    0     1     3     5    15    17    51    85   255   257   273   771
  819  1285  1365  3855  4095  4097  4369 12291 13107 20485 21845 

Action!

BYTE FUNC IsPalindrome(INT x BYTE base)
  CHAR ARRAY digits="0123456789abcdef",s(16)
  BYTE d,i,len

  len=0
  DO
    d=x MOD base
    len==+1
    s(len)=digits(d+1)
    x==/base
  UNTIL x=0
  OD
  s(0)=len

  FOR i=1 TO len/2
  DO
    IF s(i)#s(len-i+1) THEN
      RETURN (0)
    FI
  OD
RETURN (1)

PROC Main()
  INT i

  FOR i=0 TO 24999
  DO
    IF IsPalindrome(i,16)=1 AND IsPalindrome(i,4)=1 AND IsPalindrome(i,2)=1 THEN
      PrintI(i) Put(32)
    FI
  OD
RETURN
Output:

Screenshot from Atari 8-bit computer

0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

ALGOL 68

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 #
    INT max digits = BEGIN      # number of digits max number has in the smallest base #
                         INT d := 1;
                         INT v := max number;
                         WHILE v >= min base DO
                             v OVERAB min base;
                             d PLUSAB 1
                         OD;
                         d
                     END;
    # returns the digits of n in the specified base #
    PRIO DIGITS = 9;
    OP   DIGITS = ( INT n, INT base )[]INT:
         IF   INT v := ABS n;
              v < base
         THEN v # single dogit #
         ELSE   # multiple digits #
            [ 1 : max digits ]INT result;
            INT d pos := UPB result + 1;
            INT v     := ABS n;
            WHILE v > 0 DO
                result[ d pos -:= 1 ] := v MOD base;
                v OVERAB base
            OD;
            result[ d pos : UPB result ]
         FI # DIGITS # ;
    # returns TRUE if the digits in d form a palindrome, FALSE otherwise #
    OP   PALINDROMIC = ( []INT d )BOOL:
         BEGIN
             INT  left := LWB d, right := UPB d;
             BOOL is palindromic := TRUE;
             WHILE left < right AND is palindromic DO
                 is palindromic := d[ left ] = d[ right ];
                 left          +:= 1;
                 right         -:= 1
             OD;
             is palindromic
         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      #
    # 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
                IF PALINDROMIC ( n DIGITS 2 ) THEN
                    print( ( " ", whole( n, 0 ) ) )
                FI
            FI
        FI
    OD
END
Output:
 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

ALGOL W

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.
Output:
 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

APL

Works with: Dyalog APL
((/⍨)(2 4 16.((⊢≡⌽)(¯1)))¨)0,⍳24999
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

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
Output:
    0     1     3     5    15    17    51    85   255   257   273   771 
  819  1285  1365  3855  4095  4097  4369 12291 13107 20485 21845

AWK

# syntax: GAWK -f NUMBERS_IN_BASE_10_THAT_ARE_PALINDROMIC_IN_BASES_2_4_AND_16.AWK
# converted from C
BEGIN {
    start = 0
    stop = 24999
    for (i=start; i<stop; i++) {
      if (palindrome(i,2) && palindrome(i,4) && palindrome(i,16)) {
         printf("%5d%1s",i,++count%10?"":"\n")
      }
    }
    printf("\nBase 10 numbers that are palindromes in bases 2, 4, and 16: %d-%d: %d\n",start,stop,count)
    exit(0)
}
function palindrome(n,base) {
    return n == reverse(n,base)
}
function reverse(n,base,  r) {
    for (r=0; n; n=int(n/base)) {
      r = int(r*base) + n%base
    }
    return(r)
}
Output:
    0     1     3     5    15    17    51    85   255   257
  273   771   819  1285  1365  3855  4095  4097  4369 12291
13107 20485 21845
Base 10 numbers that are palindromes in bases 2, 4, and 16: 0-24999: 23

BASIC

10 DEFINT A-Z: DEFDBL R
20 FOR I=1 TO 25000
30 B=2: GOSUB 100: IF R<>I GOTO 70
40 B=4: GOSUB 100: IF R<>I GOTO 70
50 B=16: GOSUB 100: IF R<>I GOTO 70
60 PRINT I,
70 NEXT
80 END
100 R=0: N=I
110 IF N=0 THEN RETURN
120 R=R*B+N MOD B
130 N=N\B
140 GOTO 110
Output:
 0             1             3             5             15
 17            51            85            255           257
 273           771           819           1285          1365
 3855          4095          4097          4369          12291
 13107         20485         21845

BCPL

get "libhdr"
manifest $( MAXIMUM = 25000 $)

let reverse(n, base) = valof
$(  let r = 0
    while n > 0
    $(  r := r*base + n rem base
        n := n / base
    $)
    resultis r
$)

let palindrome(n, base) = n = reverse(n, base)

let start() be 
    for i = 0 to MAXIMUM
        if palindrome(i,2) & palindrome(i,4) & palindrome(i,16)
            do writef("%N*N", i)
Output:
0
1
3
5
15
17
51
85
255
257
273
771
819
1285
1365
3855
4095
4097
4369
12291
13107
20485
21845

C

#include <stdio.h>
#define MAXIMUM 25000

int reverse(int n, int base) {
    int r;
    for (r = 0; n; n /= base)
        r = r*base + n%base;
    return r;
}

int palindrome(int n, int base) {
    return n == reverse(n, base);
}

int main() {
    int i, c = 0;
    
    for (i = 0; i < MAXIMUM; i++) {
        if (palindrome(i, 2) &&
            palindrome(i, 4) &&
            palindrome(i, 16)) {
            printf("%5d%c", i, ++c % 12 ? ' ' : '\n');
        }
    }
    printf("\n");
    return 0;
}
Output:
    0     1     3     5    15    17    51    85   255   257   273   771
  819  1285  1365  3855  4095  4097  4369 12291 13107 20485 21845

COBOL

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          02 CUR-NUM           PIC 9(5).
          02 REV-BASE          PIC 99.
          02 REV-REST          PIC 9(5).
          02 REV-NEXT          PIC 9(5).
          02 REV-DGT           PIC 99.
          02 REVERSED          PIC 9(5).
       
       01 OUTPUT-FORMAT.
          02 OUT-NUM           PIC Z(4)9.
       
       PROCEDURE DIVISION.
       BEGIN.
           PERFORM 2-4-16-PALINDROME
               VARYING CUR-NUM FROM ZERO BY 1
               UNTIL CUR-NUM IS NOT LESS THAN 25000.
           STOP RUN.
       
       2-4-16-PALINDROME.
           MOVE 16 TO REV-BASE, PERFORM REVERSE THRU REV-LOOP
           IF CUR-NUM IS EQUAL TO REVERSED
               MOVE 4 TO REV-BASE, PERFORM REVERSE THRU REV-LOOP
               IF CUR-NUM IS EQUAL TO REVERSED
                   MOVE 2 TO REV-BASE, PERFORM REVERSE THRU REV-LOOP
                   IF CUR-NUM IS EQUAL TO REVERSED
                       MOVE CUR-NUM TO OUT-NUM
                       DISPLAY OUT-NUM.
       
       REVERSE.
           MOVE ZERO TO REVERSED.
           MOVE CUR-NUM TO REV-REST.
       REV-LOOP.
           IF REV-REST IS GREATER THAN ZERO
               DIVIDE REV-BASE INTO REV-REST GIVING REV-NEXT
               COMPUTE REV-DGT = REV-REST - REV-NEXT * REV-BASE
               MULTIPLY REV-BASE BY REVERSED
               ADD REV-DGT TO REVERSED
               MOVE REV-NEXT TO REV-REST
               GO TO REV-LOOP.
Output:
    0
    1
    3
    5
   15
   17
   51
   85
  255
  257
  273
  771
  819
 1285
 1365
 3855
 4095
 4097
 4369
12291
13107
20485
21845

Cowgol

include "cowgol.coh";
const MAXIMUM := 25000;

sub reverse(n: uint16, base: uint16): (r: uint16) is
    r := 0;
    while n != 0 loop
        r := r * base + n % base;
        n := n / base;
    end loop;
end sub;

var i: uint16 := 0;
var c: uint8 := 0;
while i < MAXIMUM loop
    if reverse(i,2) == i
    and reverse(i,4) == i
    and reverse(i,16) == i 
    then
        c := c + 1;
        print_i16(i);
        if c == 15 then
            print_nl();
            c := 0;
        else
            print_char(' ');
        end if;
    end if;
    i := i + 1;
end loop;
print_nl();
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
3855 4095 4097 4369 12291 13107 20485 21845

Delphi

Works with: Delphi version 6.0


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;
Output:
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


Euler

begin

    new palendromic; new n; label forN;

    palendromic
        <- ` formal n; formal base;
             begin
                 new v; new lPos; new rPos; new isPalendromic;
                 new digit;
                 label vGT0; label rGTl;
                 digit <- list 64;
                 rPos  <- 0;
                 v     <- n;
vGT0:            if v > 0 then begin
                     rPos          <- rPos + 1;
                     digit[ rPos ] <- v mod base;
                     v             <- v  %  base;
                     goto vGT0
                 end else 0;
                 isPalendromic <- true;
                 lPos          <- 1;
rGTl:            if rPos > lPos and isPalendromic then begin
                     isPalendromic <- digit[ lPos ] = digit[ rPos ];
                     lPos          <- lPos + 1;
                     rPos          <- rPos - 1;
                     goto rGTl
                 end else 0;
                 isPalendromic
             end
           '
         ;

         out 0;
         n <- -1;
forN:    if [ n <- n + 2 ] < 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 $

Output:
    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

F#

// 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 ""
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Factor

Works with: Factor version 0.99 2021-06-02
USING: io kernel math.parser prettyprint sequences ;

25,000 <iota> [
    { 2 4 16 } [ >base ] with map [ dup reverse = ] all?
] filter [ pprint bl ] each nl
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 

FreeBASIC

function ispal( byval n as integer, b as integer ) as boolean
    'determines if n is palindromic in base b
    dim as string ns
    while n
        ns += chr(48+n mod b)       'temporarily represent as a string
        n\=b
    wend
    for i as integer = 1 to len(ns)\2
        if mid(ns,i,1)<>mid(ns,len(ns)-i+1,1) then return false
    next i
    return true
end function

for i as integer = 0 to 25000
    if ispal(i,16) andalso ispal(i,4) andalso ispal(i,2) then print i;"  ";
next i
Output:
0   1   3   5   15   17   51   85   255   257   273   771   819   1285   1365   3855   4095   4097   4369   12291   13107   20485   21845

Go

Library: Go-rcu
package main

import (
    "fmt"
    "rcu"
    "strconv"
)

func reverse(s string) string {
    chars := []rune(s)
    for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
        chars[i], chars[j] = chars[j], chars[i]
    }
    return string(chars)
}

func main() {
    fmt.Println("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
    var numbers []int
    for i := int64(0); i < 25000; i++ {
        b2 := strconv.FormatInt(i, 2)
        if b2 == reverse(b2) {
            b4 := strconv.FormatInt(i, 4)
            if b4 == reverse(b4) {
                b16 := strconv.FormatInt(i, 16)
                if b16 == reverse(b16) {
                    numbers = append(numbers, int(i))
                }
            }
        }
    }
    for i, n := range numbers {
        fmt.Printf("%6s ", rcu.Commatize(n))
        if (i+1)%10 == 0 {
            fmt.Println()
        }
    }
    fmt.Println("\n\nFound", len(numbers), "such numbers.")
}
Output:
Numbers under 25,000 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  1,285  1,365  3,855  4,095  4,097  4,369 12,291 
13,107 20,485 21,845 

Found 23 such numbers.

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

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.

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)
Output:
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

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)))
Output:
1      3      5      15     17     51     85     255    257    273    771    
819    1285   1365   3855   4095   4097   4369   12291  13107  20485  21845

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
Output:
 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Mathematica/Wolfram Language

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[%]
Output:
{0, 1, 3, 5, 15, 17, 51, 85, 255, 257, 273, 771, 819, 1285, 1365, 3855, 4095, 4097, 4369, 12291, 13107, 20485, 21845}
23

Nim

import strutils, sugar

type Digit = 0..15

func toBase(n: Natural; b: Positive): seq[Digit] =
  if n == 0: return @[Digit 0]
  var n = n
  while n != 0:
    result.add n mod b
    n = n div b

func isPalindromic(s: seq[Digit]): bool =
  for i in 1..(s.len div 2):
    if s[i-1] != s[^i]: return false
  result = true

let list = collect(newSeq):
             for n in 0..<25_000:
               if n.toBase(2).isPalindromic and
                  n.toBase(4).isPalindromic and
                  n.toBase(16).isPalindromic: n

echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")
Output:
Found 23 numbers 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

Perl

Library: ntheory
use strict;
use warnings;
use ntheory 'todigitstring';

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;
Output:
1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Phix

with javascript_semantics
function palindrome(string s) return s=reverse(s) end function
function p2416(integer n)
    return palindrome(sprintf("%a",{{2,n}}))
       and palindrome(sprintf("%a",{{4,n}}))
       and palindrome(sprintf("%a",{{16,n}}))
end function
sequence res = apply(filter(tagset(25000,0),p2416),sprint)
printf(1,"%d found: %s\n",{length(res),join(res)})
Output:
23 found: 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

PL/M

100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;

DECLARE MAXIMUM LITERALLY '25$000';

/* PRINT A NUMBER */
PRINT$NUMBER: PROCEDURE (N);
    DECLARE S (7) BYTE INITIAL ('..... $');
    DECLARE (N, P) ADDRESS, C BASED P BYTE;
    P = .S(5);
DIGIT:
    P = P - 1;
    C = N MOD 10 + '0';
    N = N / 10;
    IF N > 0 THEN GO TO DIGIT;
    CALL PRINT(P);
END PRINT$NUMBER;

/* REVERSE NUMBER GIVEN BASE */
REVERSE: PROCEDURE (N, B) ADDRESS;
    DECLARE (N, R) ADDRESS, B BYTE;
    R = 0;
    DO WHILE N > 0;
        R = R*B + N MOD B;
        N = N/B;
    END;
    RETURN R;
END REVERSE;

/* CHECK IF NUMBER IS PALINDROME */
PALIN: PROCEDURE (N, B) BYTE;
    DECLARE N ADDRESS, B BYTE;
    RETURN N = REVERSE(N, B);
END PALIN;

DECLARE I ADDRESS, C BYTE;
C = 0;
DO I = 0 TO MAXIMUM;
    IF PALIN(I,2) AND PALIN(I,4) AND PALIN(I,16) THEN DO;
        CALL PRINT$NUMBER(I);
        C = C + 1;
        IF C = 15 THEN DO;
            CALL PRINT(.(13,10,'$'));
            C = 0;
        END;
    END;
END;
CALL EXIT;
EOF
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
3855 4095 4097 4369 12291 13107 20485 21845

Python

def reverse(n, base):
    r = 0
    while n > 0:
        r = r*base + n%base
        n = n//base
    return r
    
def palindrome(n, base):
    return n == reverse(n, base)
    
cnt = 0
for i in range(25000):
    if all(palindrome(i, base) for base in (2,4,16)):
        cnt += 1
        print("{:5}".format(i), end=" \n"[cnt % 12 == 0])

print()
Output:
    0     1     3     5    15    17    51    85   255   257   273   771
  819  1285  1365  3855  4095  4097  4369 12291 13107 20485 21845

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
Output:
[ 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 ]

Raku

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 } }
Output:
23 such numbers:
    0     1     3     5    15    17    51    85   255   257
  273   771   819  1285  1365  3855  4095  4097  4369 12291
13107 20485 21845

REXX

Programming note:   the conversions of a decimal number to another base (radix) was ordered such that the fastest
base conversion was performed before the other conversions.

The use of REXX's BIFs to convert decimal numbers to binary and hexadecimal were used   (instead of the   base  
function)   because they are much faster).

This REXX version takes advantage that no   even   integers need be tested   (except for the single exception:   zero),
this makes the execution twice as fast.

/*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.*/
if    n=='' |    n==","  then   n = 25000        /*Not specified?  Then use the default.*/
if cols=='' | cols==","  then cols=    10        /* "      "         "   "   "     "    */
w= 10                                            /*width of a number in any column.     */
title= ' non-negative integers that are palindromes in base 2, 4, and 16,  where  N  < ' ,
       commas(n)
say ' index │'center(title, 1 + cols*(w+1)     ) /*display the title for the output.    */
say '───────┼'center(""   , 1 + cols*(w+1), '─') /*   "     a   sep   "   "     "       */
$= right(0, w+1)                                 /*list of numbers found  (so far).     */
found= 1                                         /*# of finds (so far), the only even #.*/
idx= 1                                           /*set the IDX  (index) to unity.       */
       do j=1  by 2  to n-1                      /*find int palindromes in bases 2,4,16.*/
          h= d2x(j)                              /*convert dec. # to hexadecimal.       */
       if h\==reverse(h)          then iterate   /*Hex    number not palindromic?  Skip.*/    /* ◄■■■■■■■■ a filter. */
          b= x2b( d2x(j) ) + 0                   /*convert dec. # to hex, then to binary*/
       if b\==reverse(b)          then iterate   /*Binary number not palindromic?  Skip.*/    /* ◄■■■■■■■■ a filter. */
          q= base(j, 4)                          /*convert a decimal integer to base 4. */
       if q\==reverse(q)          then iterate   /*Base 4 number not palindromic?  Skip.*/    /* ◄■■■■■■■■ a filter. */
       found= found + 1                          /*bump number of found such numbers.   */
       $= $  right( commas(j), w)                /*add the found number  ───►  $  list. */
       if found // cols \== 0     then iterate   /*have we populated a line of output?  */
       say center(idx, 7)'│'  substr($, 2); $=   /*display what we have so far  (cols). */
       idx= idx + cols                           /*bump the  index  count for the output*/
       end   /*j*/

if $\==''  then say center(idx, 7)"│"  substr($, 2)  /*possible display residual output.*/
say '───────┴'center(""   , 1 + cols*(w+1), '─')     /*display the foot sep for output. */
say
say 'Found '          found          title
exit 0                                           /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?;  do jc=length(?)-3  to 1  by -3; ?=insert(',', ?, jc); end;  return ?
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
output   when using the default inputs:
 index │             non-negative integers that are palindromes in base 2, 4, and 16,  where  N  <  25,000
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │          0          1          3          5         15         17         51         85        255        257
  11   │        273        771        819      1,285      1,365      3,855      4,095      4,097      4,369     12,291
  21   │     13,107     20,485     21,845
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Found  23  non-negative integers that are palindromes in base 2, 4, and 16,  where  N  <  25,000

Ring

load "stdlib.ring"
see "working..." + nl
see "Numbers in base 10 that are palindromic in bases 2, 4, and 16:" + nl

row = 0
limit = 25000

for n = 1 to limit
    base2 = decimaltobase(n,2)
    base4 = decimaltobase(n,4)
    base16 = hex(n)
    bool = ispalindrome(base2) and ispalindrome(base4) and ispalindrome(base16)
    if bool = 1
       see "" + n + " "
       row = row + 1
       if row%5 = 0
          see nl
       ok
    ok
next

see nl + "Found " + row + " numbers" + nl
see "done..." + nl

func decimaltobase(nr,base)
     decList = 0:15
     baseList = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]

     binList = [] 
     binary = 0
     remainder = 1
     while(nr != 0)
          remainder = nr % base
          ind = find(decList,remainder)
          rem = baseList[ind]
          add(binList,rem)
          nr = floor(nr/base) 
     end
     binlist = reverse(binList)
     binList = list2str(binList)
     binList = substr(binList,nl,"")  
     return binList
Output:
working...
Numbers in base 10 that are palindromic in bases 2, 4, and 16:
1 3 5 15 17 
51 85 255 257 273 
771 819 1285 1365 3855 
4095 4097 4369 12291 13107 
20485 21845 
Found 22 numbers
done...

RPL

Works with: HP version 48

Brute force

« "" 
  OVER SIZE 1 FOR j
    OVER j DUP SUB + 
  NEXT SWAP DROP
» 'REVSTR' STO 

« → base 
  « ""
    WHILE OVER REPEAT
       SWAP base MOD LASTARG / IP
       "0123456789ABCDEF" ROT 1 + DUP SUB ROT +
    END SWAP DROP
» » 'D→B' STO 

« CASE
     HEX DUP R→B →STR 3 OVER SIZE SUB DUP REVSTRTHEN DROP 0 END
     DUP 4 D→B DUP REVSTRTHEN DROP 0 END
     BIN DUP R→B →STR 3 OVER SIZE SUB DUP REVSTR ==
  END
» 'PAL2416' STO 

« { 0 }
  1 25000 FOR n
     IF n PAL2416 THEN n + END
  2 STEP
» 'TASK' 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?
» 'PAL24?' STO

« HEX R→B →STR → h 
  « "#"
    h SIZE 1 - 3 FOR j 
       h j DUP SUB + 
    -1 STEP
    "h" + STR→ B→R
» » ‘REVHEX’ STO

« { }
 0 15 FOR b 
    IF b PAL24? 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 REVHEX + 
          IF DUP PAL24? THEN +
          ELSE IF 25000 ≥ THEN KILL END  @ not idiomatic but useful to exit 3 nested loops
          END
 NEXT NEXT NEXT
 SORT
» 'TASK' STO 
TASK SORT

Runs in 2 minutes 16 on a HP-48SX: 18 times faster than brute force!

Output:
1: { 0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845 }

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(" ")
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Seed7

$ include "seed7_05.s7i";

const func boolean: palindrome (in string: input) is
  return input = reverse(input);

const proc: main is func
  local
    var integer: n is 1;
  begin
    write("0 ");
    for n range 1 to 24999 step 2 do
      if palindrome(n radix 2) and palindrome(n radix 4) and palindrome(n radix 16) then
        write(n <& " ");
      end if;
    end for;
  end func;
Output:
0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365 3855 4095 4097 4369 12291 13107 20485 21845

Sidef

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) }
    }
}
Output:
[0, 1, 3, 5, 15, 17, 51, 85, 255, 257, 273, 771, 819, 1285, 1365, 3855, 4095, 4097, 4369, 12291, 13107, 20485, 21845]

Wren

Library: Wren-fmt
import "./fmt" for Conv, Fmt

System.print("Numbers under 25,000 in base 10 which are palindromic in bases 2, 4 and 16:")
var numbers = []
for (i in 0..24999) {
    var b2 = Conv.itoa(i, 2)
    if (b2 == b2[-1..0]) {
        var b4 = Conv.itoa(i, 4)
        if (b4 == b4[-1..0]) {
            var b16 = Conv.itoa(i, 16)
            if (b16 == b16[-1..0]) numbers.add(i)
        }
    }
}
Fmt.tprint("$,6d", numbers, 8)
System.print("\nFound %(numbers.count) such numbers.")
Output:
Numbers under 25,000 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  1,285  1,365  3,855
 4,095  4,097  4,369 12,291 13,107 20,485 21,845

Found 23 such numbers.

XPL0

func Reverse(N, Base);  \Reverse order of digits in N for given Base
int  N, Base, M;
[M:= 0;
repeat  N:= N/Base;
        M:= M*Base + rem(0);
until   N=0;
return M;
];

int Count, N;
[Count:= 0;
for N:= 1 to 25000-1 do
    if N = Reverse(N, 2) &
       N = Reverse(N, 4) &
       N = Reverse(N, 16) then
        [IntOut(0, N);
        Count:= Count+1;
        if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
        ];
CrLf(0);
IntOut(0, Count);
Text(0, " such numbers found.
");
]
Output:
1       3       5       15      17      51      85      255     257     273
771     819     1285    1365    3855    4095    4097    4369    12291   13107
20485   21845   
22 such numbers found.