Numbers in base 10 that are palindromic in bases 2, 4, and 16: Difference between revisions
Numbers in base 10 that are palindromic in bases 2, 4, and 16 (view source)
Revision as of 00:04, 28 August 2022
, 1 year agosyntax highlighting fixup automation
(→{{header|J}}: base 4, not base 8) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8:
{{trans|Python}}
<
V r = 0
L n > 0
Line 24:
print(‘#5’.format(i), end' " \n"[cnt % 12 == 0])
print()</
{{out}}
Line 33:
=={{header|Action!}}==
<
CHAR ARRAY digits="0123456789abcdef",s(16)
BYTE d,i,len
Line 64:
FI
OD
RETURN</
{{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}}==
<
INT max number = 25 000; # maximum number to consider #
INT min base = 2; # smallest base needed #
Line 122:
FI
OD
END</
{{out}}
<pre>
Line 130:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
{{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">
# syntax: GAWK -f NUMBERS_IN_BASE_10_THAT_ARE_PALINDROMIC_IN_BASES_2_4_AND_16.AWK
# converted from C
Line 157:
return(r)
}
</syntaxhighlight>
{{out}}
<pre>
Line 167:
=={{header|BASIC}}==
<
20 FOR I=1 TO 25000
30 B=2: GOSUB 100: IF R<>I GOTO 70
Line 179:
120 R=R*B+N MOD B
130 N=N\B
140 GOTO 110</
{{out}}
<pre> 0 1 3 5 15
Line 188:
=={{header|BCPL}}==
<
manifest $( MAXIMUM = 25000 $)
Line 205:
for i = 0 to MAXIMUM
if palindrome(i,2) & palindrome(i,4) & palindrome(i,16)
do writef("%N*N", i)</
{{out}}
<pre>0
Line 232:
=={{header|C}}==
<
#define MAXIMUM 25000
Line 258:
printf("\n");
return 0;
}</
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
Line 264:
=={{header|COBOL}}==
<
PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
Line 307:
ADD REV-DGT TO REVERSED
MOVE REV-NEXT TO REV-REST
GO TO REV-LOOP.</
{{out}}
<pre> 0
Line 334:
=={{header|Cowgol}}==
<
const MAXIMUM := 25000;
Line 363:
i := i + 1;
end loop;
print_nl();</
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
Line 369:
=={{header|F_Sharp|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 ""
</syntaxhighlight>
{{out}}
<pre>
Line 381:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
25,000 <iota> [
{ 2 4 16 } [ >base ] with map [ dup reverse = ] all?
] filter [ pprint bl ] each nl</
{{out}}
<pre>
Line 392:
=={{header|FreeBASIC}}==
<
'determines if n is palindromic in base b
dim as string ns
Line 407:
for i as integer = 0 to 25000
if ispal(i,16) andalso ispal(i,4) andalso ispal(i,2) then print i;" ";
next i</
{{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}}
<
import (
Line 450:
}
fmt.Println("\n\nFound", len(numbers), "such numbers.")
}</
{{out}}
Line 463:
=={{header|J}}==
<
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|Julia}}==
<
foreach(p -> print(rpad(p[2], 7), p[1] % 11 == 0 ? "\n" : ""), enumerate(filter(palinbases, 1:25000)))
</
<pre>
1 3 5 15 17 51 85 255 257 273 771
Line 479:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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[%]</
{{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}
Line 489:
=={{header|Nim}}==
<
type Digit = 0..15
Line 512:
echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")</
{{out}}
Line 520:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use ntheory 'todigitstring';
Line 526:
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;</
{{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}}==
<!--<
<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 541:
<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>
<!--</
{{out}}
<pre>
Line 548:
=={{header|PL/M}}==
<
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
Line 599:
END;
CALL EXIT;
EOF</
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
Line 605:
=={{header|Python}}==
<
r = 0
while n > 0:
Line 621:
print("{:5}".format(i), end=" \n"[cnt % 12 == 0])
print()</
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
Line 627:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
(^25000).grep: -> $n { all (2,4,16).map: { $n.base($_) eq $n.base($_).flip } }</
{{out}}
<pre>23 such numbers:
Line 644:
This REXX version takes advantage that no ''even'' integers need be tested (except for the single exception: zero),
<br>this makes the execution twice as fast.
<
numeric digits 100 /*ensure enough dec. digs for large #'s*/
parse arg n cols . /*obtain optional argument from the CL.*/
Line 681:
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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 695:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 738:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
{{out}}
<pre>
Line 753:
=={{header|Seed7}}==
<
const func boolean: palindrome (in string: input) is
Line 768:
end if;
end for;
end func;</
{{out}}
<pre>
Line 775:
=={{header|Sidef}}==
<
for (var k = 0; k < 25_000; k = k.next_palindrome(16)) {
take(k) if [2, 4].all{|b| k.is_palindrome(b) }
}
}</
{{out}}
<pre>
Line 788:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<
import "/seq" for Lst
Line 804:
}
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$,6d", chunk)
System.print("\nFound %(numbers.count) such numbers.")</
{{out}}
Line 817:
=={{header|XPL0}}==
<
int N, Base, M;
[M:= 0;
Line 840:
Text(0, " such numbers found.
");
]</
{{out}}
|