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

Added Algol 68
(Added XPL0 example.)
(Added Algol 68)
Line 4:
 
<br><br>
 
=={{header|ALGOL 68}}==
<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 #
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 primes in bases 2, 4 and 16 #
FOR n FROM 0 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</lang>
{{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}}==
3,048

edits