Palindromic primes in base 16: Difference between revisions

(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
Line 216:
 
Found 13 such primes.
</pre>
 
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry uses a generator that produces an unbounded stream of arrays of the form [dec, hex], where `dec` is the palindromic prime as a JSON number, and `hex` is the JSON string corresponding to its hexadecimal representation.
 
For a suitable implementation of `is_prime`, see e.g. [[Erd%C5%91s-primes#jq]].
<lang jq>
# '''Preliminaries'''
 
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
 
# decimal number to exploded hex array
def exploded_hex:
def stream:
recurse(if . > 0 then ./16|floor else empty end) | . % 16 ;
if . == 0 then [48]
else [stream] | reverse | .[1:]
| map(if . < 10 then 48 + . else . + 87 end)
end;
</lang>
'''The Task'''
<lang jq># Output: a stream of [decimal, hexadecimal] values
def palindromic_primes_in_base_16:
(2, (range(3; infinite; 2) | select(is_prime)))
| exploded_hex as $hex
|select( $hex | (. == reverse))
| [., ($hex|implode)] ;
 
emit_until(.[0] >= 500; palindromic_primes_in_base_16)</lang>
{{out}}
<pre>
[2,"2"]
[3,"3"]
[5,"5"]
[7,"7"]
[11,"b"]
[13,"d"]
[17,"11"]
[257,"101"]
[337,"151"]
[353,"161"]
[401,"191"]
[433,"1b1"]
[449,"1c1"]
</pre>
 
Line 228 ⟶ 276:
foreach(s -> print(s, " "), palprimes(500, 16)) # 2 3 5 7 b d 11 101 151 161 191 1b1 1c1
</lang>
 
 
=={{header|Nim}}==
2,472

edits