Ormiston pairs: Difference between revisions

(Added Rust solution)
Line 342:
+/isorm i.&.(p:inv) 1e7 NB. number of Ormiston pairs less than 1e7
3722</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Preliminaries'''
<syntaxhighlight lang=jq>
# return an array, $a, of length .+1 or .+2 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase(i):
if .[i] then
reduce range(2; (1 + length) / i) as $j (.; .[i * $j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i)) ;
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang=jq>
def digits: tostring | explode;
 
def ormiston_pairs($limit):
($limit | primeSieve | map(select(.))) as $primes
| range(0; $primes|length-1) as $i
| $primes[$i] as $p1
| $primes[$i+1] as $p2
| select( ($p2|digits|sort) == ($p1|digits|sort) )
| [$p1, $p2] ;
 
def task($limit):
reduce ormiston_pairs($limit) as $pair (
{count:0, orm30: [], counts: [], j: 1e5};
if .count < 30 then .orm30 += [$pair] else . end
| if $pair[0] >= .j
then .counts += [.count]
| .j *= 10
else .
end
| .count += 1 )
| .counts += [.count]
| ("First 30 Ormiston pairs:", (.orm30 | map(tostring) | _nwise(3) | join(" "))),
"",
foreach range(0; .counts|length) as $i (.j = 1e5;
.emit = "\(.counts[$i]) Ormiston pairs before \(.j)"
| .j *= 10;
select(.emit).emit) );
 
task(1e7) # ten million
</syntaxhighlight>
{{output}}
<pre>
First 30 Ormiston pairs:
[1913,1931] [18379,18397] [19013,19031]
[25013,25031] [34613,34631] [35617,35671]
[35879,35897] [36979,36997] [37379,37397]
[37813,37831] [40013,40031] [40213,40231]
[40639,40693] [45613,45631] [48091,48109]
[49279,49297] [51613,51631] [55313,55331]
[56179,56197] [56713,56731] [58613,58631]
[63079,63097] [63179,63197] [64091,64109]
[65479,65497] [66413,66431] [74779,74797]
[75913,75931] [76213,76231] [76579,76597]
 
40 Ormiston pairs before 100000
382 Ormiston pairs before 1000000
3722 Ormiston pairs before 10000000
</pre>
 
=={{header|Phix}}==
2,442

edits