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

m
syntax highlighting fixup automation
(→‎{{header|J}}: base 4, not base 8)
m (syntax highlighting fixup automation)
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 122:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 130:
=={{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|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:
return(r)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 167:
 
=={{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:
120 R=R*B+N MOD B
130 N=N\B
140 GOTO 110</langsyntaxhighlight>
{{out}}
<pre> 0 1 3 5 15
Line 188:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
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)</langsyntaxhighlight>
{{out}}
<pre>0
Line 232:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#define MAXIMUM 25000
 
Line 258:
printf("\n");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 0 1 3 5 15 17 51 85 255 257 273 771
Line 264:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. PALINDROMIC-BASE-2-4-16.
Line 307:
ADD REV-DGT TO REVERSED
MOVE REV-NEXT TO REV-REST
GO TO REV-LOOP.</langsyntaxhighlight>
{{out}}
<pre> 0
Line 334:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 25000;
 
Line 363:
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
Line 369:
 
=={{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:
=={{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:
 
=={{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:
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:
}
fmt.Println("\n\nFound", len(numbers), "such numbers.")
}</langsyntaxhighlight>
 
{{out}}
Line 463:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="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>
</lang>
 
=={{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
Line 479:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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[%]</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}
Line 489:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
type Digit = 0..15
Line 512:
 
echo "Found ", list.len, " numbers which are palindromic in bases 2, 4 and 16:"
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 520:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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;</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 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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 548:
 
=={{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 599:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>0 1 3 5 15 17 51 85 255 257 273 771 819 1285 1365
Line 605:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def reverse(n, base):
r = 0
while n > 0:
Line 621:
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
Line 627:
 
=={{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 644:
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 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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 695:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 738:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 753:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: palindrome (in string: input) is
Line 768:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 775:
 
=={{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 788:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
import "/seq" for Lst
 
Line 804:
}
for (chunk in Lst.chunks(numbers, 8)) Fmt.print("$,6d", chunk)
System.print("\nFound %(numbers.count) such numbers.")</langsyntaxhighlight>
 
{{out}}
Line 817:
 
=={{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 840:
Text(0, " such numbers found.
");
]</langsyntaxhighlight>
 
{{out}}
10,327

edits