Special factorials: Difference between revisions
Content added Content deleted
(Added Sidef) |
|||
Line 669: | Line 669: | ||
none <- rf(119) |
none <- rf(119) |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
{{trans|Wren}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
The standard version of jq does not maintain integer precision sufficiently for |
|||
some of the specific tasks (some of the hyperfactorials and the computation of 5$) |
|||
but can otherwise be used. |
|||
<lang jq> |
|||
# for integer precision: |
|||
def power($b): . as $a | reduce range(0;$b) as $i (1; . * $a); |
|||
def sf: |
|||
. as $n |
|||
| if $n < 2 then 1 |
|||
else {sfact: 1, fact: 1} |
|||
| reduce range (2;1+$n) as $i (.; |
|||
.fact *= $i |
|||
| .sfact *= .fact) |
|||
| .sfact |
|||
end; |
|||
def H: |
|||
. as $n |
|||
| if $n < 2 then 1 |
|||
else |
|||
reduce range(2;1+$n) as $i ( {hfact: 1}; |
|||
.hfact *= ($i | power($i))) |
|||
| .hfact |
|||
end; |
|||
def af: |
|||
. as $n |
|||
| if $n < 1 then 0 |
|||
else {afact: 0, fact: 1, sign: (if $n%2 == 0 then -1 else 1 end)} |
|||
| reduce range(1; 1+$n) as $i (.; |
|||
.fact *= $i |
|||
| .afact += .fact * .sign |
|||
| .sign *= -1) |
|||
| .afact |
|||
end; |
|||
def ef: # recursive |
|||
. as $n |
|||
| if $n < 1 then 1 |
|||
else $n | power( ($n-1)|ef ) |
|||
end; |
|||
def rf: |
|||
. as $n |
|||
| label $out |
|||
| foreach range(0; infinite) as $i ( {fact: 1, emit: false}; |
|||
if (.fact == $n) then .emit = $i |
|||
elif .fact > $n then .emit = null |
|||
else .fact *= ($i + 1) |
|||
end; |
|||
if .emit or (.emit == null) then .emit, break $out else empty end) ; |
|||
</lang> |
|||
''' The tasks:''' |
|||
<lang jq> |
|||
"First 10 superfactorials:", |
|||
(range(0;10) | sf), |
|||
"\nFirst 10 hyperfactorials:", |
|||
(range(0; 10) | H), |
|||
"\nFirst 10 alternating factorials:", |
|||
(range(0;10) | af), |
|||
"\n\nFirst 5 exponential factorials:", |
|||
(range(0;5) | ef), |
|||
"\nThe number of digits in 5$ is \(5 | ef | tostring | length)", |
|||
"\nReverse factorials:", |
|||
( 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 119 | "\(rf) <- rf(\(.))") |
|||
</lang> |
|||
{{out}} |
|||
Invocation: gojq -nr -f special-factorials.jq |
|||
The output is essentially as for [[#Wren|Wren]] and so is not repeated here. |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |