Jump to content

Concatenate two primes is also prime: Difference between revisions

(added AWK)
Line 277:
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<lang jq>def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else {i:23}
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
| .i * .i > $n
end;
 
# Emit an array of primes less than `.`
def primes:
if . < 2 then []
else
[2] + [range(3; .; 2) | select(is_prime)]
end;
 
# Pretty-printing
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</lang>
'''The task'''
<lang jq># Emit [p1,p2] where p1 < p2 < . and the concatenation is prime
def concatenative_primes:
primes
| range(0;length) as $i
| range($i+1;length) as $j
| [.[$i], .[$j]], [.[$j], .[$i]]
| select( map(tostring) | add | tonumber | is_prime);
 
[100 | concatenative_primes | join("||")]
| (nwise(10) | map(lpad(6)) | join(" "))</lang>
{{out}}
<pre>
2||3 2||11 2||23 2||29 2||41 2||71 2||83 5||3 3||7 7||3
3||11 11||3 3||13 3||17 17||3 19||3 23||3 29||3 3||31 31||3
3||37 37||3 43||3 3||47 3||53 3||59 59||3 61||3 3||67 67||3
3||73 73||3 3||79 3||83 3||89 3||97 5||23 5||41 5||47 5||71
13||7 7||19 19||7 31||7 7||43 7||61 61||7 67||7 7||73 79||7
7||97 97||7 11||17 11||23 23||11 11||29 41||11 11||53 11||71 83||11
13||19 19||13 13||61 61||13 13||67 13||73 17||23 29||17 17||41 17||47
17||53 17||59 17||83 83||17 17||89 19||31 31||19 37||19 67||19 19||73
19||79 79||19 19||97 97||19 23||41 23||47 47||23 53||23 59||23 23||71
23||83 23||89 89||23 41||29 47||29 29||53 29||71 71||29 83||29 89||29
31||37 61||31 31||67 73||31 43||37 37||61 37||67 67||37 37||79 79||37
37||97 41||53 41||59 89||41 61||43 43||73 43||97 97||43 53||47 47||59
47||83 47||89 59||53 83||53 71||59 67||61 61||73 61||97 67||79 97||67
</pre>
=={{header|Julia}}==
<lang julia>using Primes
Line 291 ⟶ 353:
foreach(p -> print(lpad(last(p), 5), first(p) % 16 == 0 ? "\n" : ""),
catprimes() |> enumerate)
</lang>{{out}}<pre>
<pre>
23 37 53 73 113 137 173 193 197 211 223 229 233 241 271 283
293 311 313 317 331 337 347 353 359 367 373 379 383 389 397 433
2,507

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.