Product of divisors: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added a foot separator to the outputs.) |
|||
Line 740: | Line 740: | ||
2116 47 254803968 343 125000</pre> |
2116 47 254803968 343 125000</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
gojq should be used if integer precision is to be guaranteed. |
|||
Since a `divisors` function is more likely to be generally useful than a "product of divisors" |
|||
function, this entry implements the latter in terms of the former. |
|||
<lang jq># divisors as an unsorted stream |
|||
def divisors: |
|||
if . == 1 then 1 |
|||
else . as $n |
|||
| label $out |
|||
| range(1; $n) as $i |
|||
| ($i * $i) as $i2 |
|||
| if $i2 > $n then break $out |
|||
else if $i2 == $n |
|||
then $i |
|||
elif ($n % $i) == 0 |
|||
then $i, ($n/$i) |
|||
else empty |
|||
end |
|||
end |
|||
end; |
|||
def product(s): reduce s as $x (1; . * $x); |
|||
def product_of_divisors: product(divisors); |
|||
# For pretty-printing |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
</lang> |
|||
'''Example''' |
|||
<lang jq>"n product of divisors", |
|||
(range(100; 110) | "\(.) \(product_of_divisors|lpad(15))")</lang> |
|||
{{out}} |
|||
<pre> |
|||
n product of divisors |
|||
100 1000000000 |
|||
101 101 |
|||
102 108243216 |
|||
103 103 |
|||
104 116985856 |
|||
105 121550625 |
|||
106 11236 |
|||
107 107 |
|||
108 1586874322944 |
|||
109 109 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang julia>using Primes |
<lang julia>using Primes |