26 lines
823 B
Text
26 lines
823 B
Text
# The def of _nwise can be omitted if using the C implementation of jq.
|
|
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) + .;
|
|
|
|
# tabular print
|
|
def tprint(columns; wide):
|
|
reduce _nwise(columns) as $row ("";
|
|
. + ($row|map(lpad(wide)) | join(" ")) + "\n" );
|
|
|
|
# Input: a positive integer
|
|
# Output: an array, $a, of length .+1 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*$i; length; $i)) as $j (.; .[$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)) ;
|