Palindromic primes in base 16: Difference between revisions

m
(Added Arturo implementation)
m (→‎{{header|Wren}}: Minor tidy)
(5 intermediate revisions by 5 users not shown)
Line 6:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 20:
I h == reversed(h) & is_prime(n)
print(h, end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 29:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC Palindrome(CHAR ARRAY s)
Line 88:
OD
PrintF("%EThere are %I palindromic primes",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Palindromic_primes_in_base_16.png Screenshot from Atari 8-bit computer]
Line 99:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find primes that are palendromic in base 16 #
# sieve the primes to 499 #
PR read "primes.incl.a68" PR
Line 145:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
2 3 5 7 B D 11 101 151 161 191 1B1 1C1
</pre>
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on isPrime(n)
if ((n < 4) or (n is 5)) then return (n > 1)
if ((n mod 2 = 0) or (n mod 3 = 0) or (n mod 5 = 0)) then return false
repeat with i from 7 to (n ^ 0.5) div 1 by 30
if ((n mod i = 0) or (n mod (i + 4) = 0) or (n mod (i + 6) = 0) or ¬
(n mod (i + 10) = 0) or (n mod (i + 12) = 0) or (n mod (i + 16) = 0) or ¬
(n mod (i + 22) = 0) or (n mod (i + 24) = 0)) then return false
end repeat
return true
end isPrime
 
on task()
set digits to "0123456789ABCDEF"'s characters
set output to {"2"} -- Take "2" as read.
repeat with n from 3 to 499 by 2 -- All other primes are odd.
if (isPrime(n)) then
-- Only the number's hex digit /values/ are needed for testing.
set vals to {}
repeat until (n = 0)
set vals's beginning to n mod 16
set n to n div 16
end repeat
-- If they're palindromic, build a text representation and append this to the output.
if (vals = vals's reverse) then
set hex to digits's item ((vals's beginning) + 1)
repeat with i from 2 to (count vals)
set hex to hex & digits's item ((vals's item i) + 1)
end repeat
set output's end to hex
end if
end if
end repeat
return output
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{"2", "3", "5", "7", "B", "D", "11", "101", "151", "161", "191", "1B1", "1C1"}</syntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">palindrome?: function [a][
and? -> prime? a
-> equal? digits.base:16 a reverse digits.base:16 a
]
 
print map select 1..500 => palindrome? 'x -> upper as.hex x</langsyntaxhighlight>
 
{{out}}
Line 165 ⟶ 209:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PALINDROMIC_PRIMES_IN_BASE_16.AWK
BEGIN {
Line 196 ⟶ 240:
return(rts)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 206 ⟶ 250:
=={{header|C++}}==
See [[Palindromic primes#C.2B.2B|C++ solution for task 'Palindromic Primes']].
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
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;
 
 
 
procedure ShowPalindromePrimes16(Memo: TMemo);
var I: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=1 to 1000-1 do
if IsPrime(I) then
if IsPalindrome(I,16) then
begin
Inc(Cnt);
S:=S+Format('%4X',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
 
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 B
D 11 101 151 161
191 1B1 1C1 313 373
3B3
Count=16
Elapsed Time: 2.116 ms.
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
let rec fN g=[yield g%16; if g>15 then yield! fN(g/16)]
primes32()|>Seq.takeWhile((>)500)|>Seq.filter(fun g->let g=fN g in List.rev g=g)|>Seq.iter(printf "%0x "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 218 ⟶ 334:
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math.parser math.primes prettyprint sequences
sequences.extras ;
 
500 primes-upto [ >hex ] [ dup reverse = ] map-filter .</langsyntaxhighlight>
{{out}}
<pre>
Line 243 ⟶ 359:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Function isprime(num As Ulongint) As Boolean
For i As Integer = 2 To Sqr(num)
Line 273 ⟶ 389:
Print !"\n\nEncontrados"; cont; " primos palindrómicos entre " & inicio & " y " & final
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>2 3 5 7 B D 11 101 151 161 191 1B1 1C1
Line 282 ⟶ 398:
=={{header|Go}}==
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 314 ⟶ 430:
}
fmt.Println("\n\nFound", count, "such primes.")
}</langsyntaxhighlight>
 
{{out}}
Line 326 ⟶ 442:
</pre>
 
 
=={{header|J}}==
 
<syntaxhighlight lang=J> palindromic16=: (-: |.)@hfd@>
hfd@> (#~ palindromic16) p: i. p:inv 500
2
3
5
7
b
d
11
101
151
161
191
1b1
1c1</syntaxhighlight>
 
=={{header|jq}}==
Line 334 ⟶ 468:
 
For a suitable implementation of `is_prime`, see e.g. [[Erd%C5%91s-primes#jq]].
<syntaxhighlight lang="jq">
<lang jq>
# '''Preliminaries'''
 
Line 347 ⟶ 481:
| map(if . < 10 then 48 + . else . + 87 end)
end;
</syntaxhighlight>
</lang>
'''The Task'''
<langsyntaxhighlight lang="jq"># Output: a stream of [decimal, hexadecimal] values
def palindromic_primes_in_base_16:
(2, (range(3; infinite; 2) | select(is_prime)))
Line 356 ⟶ 490:
| [., ($hex|implode)] ;
 
emit_until(.[0] >= 500; palindromic_primes_in_base_16)</langsyntaxhighlight>
{{out}}
<pre>
Line 373 ⟶ 507:
[449,"1c1"]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
ispal(n, base) = begin dig = digits(n, base=base); dig == reverse(dig) end
Line 383 ⟶ 516:
 
foreach(s -> print(s, " "), palprimes(500, 16)) # 2 3 5 7 b d 11 101 151 161 191 1b1 1c1
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Giving the base 10 numbers and the base 16 numbers:
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[499], PrimeQ[#] \[And] PalindromeQ[IntegerDigits[#, 16]] &]
BaseForm[%, 16]</langsyntaxhighlight>
{{out}}
<pre>
Line 396 ⟶ 529:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
func isPalindromic(s: string): bool =
Line 422 ⟶ 555:
 
echo "Found ", list.len, " palindromic primes in base 16:"
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 429 ⟶ 562:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"> (1 x $_ ) !~ /^(11+)\1+$/ # test if prime
and $h = sprintf "%x", $_ # convert to hex
and $h eq reverse $h # palindromic?
and print "$h " # much rejoicing
for 1..500;</langsyntaxhighlight>
{{out}}
<pre>1 2 3 5 7 b d 11 101 151 161 191 1b1 1c1</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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%x"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">palindrome</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;">"found %d: %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 453 ⟶ 586:
See [[Palindromic primes#Quackery]] for rest of code. This is a trivial modification.
 
<langsyntaxhighlight Quackerylang="quackery">16 base put
500 times
[ i^ isprime if
[ i^ digits palindromic if
[ i^ echo sp ] ] ]
base release</langsyntaxhighlight>
 
{{out}}
Line 467 ⟶ 600:
=={{header|Raku}}==
Trivial modification of [[Palindromic_primes#Raku|Palindromic primes]] task.
<syntaxhighlight lang="raku" perl6line>say "{+$_} matching numbers:\n{.batch(10)».fmt('%3X').join: "\n"}"
given (^500).grep: { .is-prime and .base(16) eq .base(16).flip };</langsyntaxhighlight>
{{out}}
<pre>13 matching numbers:
Line 475 ⟶ 608:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays hexadecimal palindromic primes for all N < 500. */
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 500 /*Not specified? Then use the default.*/
Line 516 ⟶ 649:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 529 ⟶ 662:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 549 ⟶ 682:
see nl + "Found " + row + " palindromic primes in base 16" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 559 ⟶ 692:
Found 13 palindromic primes in base 16
done...
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">res = Prime.each(500).filter_map do |pr|
str = pr.to_s(16)
str if str == str.reverse
end
puts res.join(", ")</syntaxhighlight>
{{out}}
<pre>2, 3, 5, 7, b, d, 11, 101, 151, 161, 191, 1b1, 1c1
</pre>
 
Line 565 ⟶ 707:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
 
Line 600 ⟶ 742:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 607 ⟶ 749:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) {
var list = []
for (var p = 2; p <= upto; p = p.next_palindrome(base)) {
Line 619 ⟶ 761:
list.each {|p|
say "#{'%3s' % p}_10 = #{'%3s' % p.base(16)}_16"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 640 ⟶ 782:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./fmt" for Conv, Fmt
 
System.print("Primes < 500 which are palindromic in base 16:")
Line 654 ⟶ 796:
}
}
System.print("\n\nFound %(count) such primes.")</langsyntaxhighlight>
 
{{out}}
Line 667 ⟶ 809:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 697 ⟶ 839:
Text(0, " such numbers found.
");
]</langsyntaxhighlight>
 
{{out}}
9,487

edits