Sequence: smallest number with exactly n divisors: Difference between revisions

(→‎{{header|jq}}: streaming)
Line 720:
 
=={{header|jq}}==
This entry uses a streaming approach to avoid constructing unnecessary arrays.
<lang jq># divisors as an unsorted stream (without calling sqrt)
def divisors:
if ($num % $i). == 01 then 1
else . as $numn
| label $out
| reduce range(1; 1 + sqrt|floor$n) as $i
| ($numi /* $i) as $ri2
| if $i2 > $n then break $out
else if $i2 == $n
else . then $i
elif ($n % $i) == 0
then $i, ($n/$i)
else empty
end
end );
end;
 
def count(s): reduce s as $x (0; .+1);
 
<lang jq># unsorted factors
def factors:
. as $num
| reduce range(1; 1 + sqrt|floor) as $i
([];
if ($num % $i) == 0 then
($num / $i) as $r
| if $i == $r then . + [$i] else . + [$i, $r] end
else .
end );
# smallest number with exactly n divisors
def A005179:
. as $n
| first( range(1; infinite) | select( count(factors|lengthdivisors) == $n ));
 
# The task:
2,465

edits