Special factorials: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 52: | Line 52: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F sf(n) |
||
V result = BigInt(1) |
V result = BigInt(1) |
||
L(i) 2 .. n |
L(i) 2 .. n |
||
Line 107: | Line 107: | ||
L(n) [1, 2, 6, 24, 119, 120, 720, 5040, 40320, 362880, 3628800] |
L(n) [1, 2, 6, 24, 119, 120, 720, 5040, 40320, 362880, 3628800] |
||
V r = rf(n) |
V r = rf(n) |
||
print(f:‘{n:7}: {I r >= 0 {f:‘{r:2}’} E ‘undefined’}’)</ |
print(f:‘{n:7}: {I r >= 0 {f:‘{r:2}’} E ‘undefined’}’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 133: | Line 133: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <math.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 270: | Line 270: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 9 super factorials: |
<pre>First 9 super factorials: |
||
Line 298: | Line 298: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="cpp">#include <cmath> |
||
#include <cstdint> |
#include <cstdint> |
||
#include <iostream> |
#include <iostream> |
||
Line 417: | Line 417: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 9 super factorials |
<pre>First 9 super factorials |
||
Line 445: | Line 445: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting io kernel math math.factorials math.functions |
||
math.parser math.ranges prettyprint sequences sequences.extras ; |
math.parser math.ranges prettyprint sequences sequences.extras ; |
||
IN: rosetta-code.special-factorials |
IN: rosetta-code.special-factorials |
||
Line 480: | Line 480: | ||
{ 1 2 6 24 120 720 5040 40320 362880 3628800 119 } |
{ 1 2 6 24 120 720 5040 40320 362880 3628800 119 } |
||
[ dup rf "rf(%d) = %u\n" printf ] each nl</ |
[ dup rf "rf(%d) = %u\n" printf ] each nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 512: | Line 512: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
<lang>Function Sf(n) = Prod<k=1, n>[k!]. |
<syntaxhighlight lang="text">Function Sf(n) = Prod<k=1, n>[k!]. |
||
Function H(n) = Prod<k=1, n>[k^k]. |
Function H(n) = Prod<k=1, n>[k^k]. |
||
Line 532: | Line 532: | ||
!!' '; |
!!' '; |
||
for n=1 to 10 do !!Rf(n!) od; |
for n=1 to 10 do !!Rf(n!) od; |
||
!!Rf(119)</ |
!!Rf(119)</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Only goes up to H(7) due to overflow. Using a library with big int support is possible, but would only add bloat without being illustrative. |
Only goes up to H(7) due to overflow. Using a library with big int support is possible, but would only add bloat without being illustrative. |
||
< |
<syntaxhighlight lang="freebasic">function factorial(n as uinteger) as ulongint |
||
if n<2 then return 1 else return n*factorial(n-1) |
if n<2 then return 1 else return n*factorial(n-1) |
||
end function |
end function |
||
Line 589: | Line 589: | ||
print rf(factorial(n));" "; |
print rf(factorial(n));" "; |
||
next n |
next n |
||
print rf(119)</ |
print rf(119)</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 703: | Line 703: | ||
fmt.Printf("%4s <- rf(%d)\n", srfact, fact) |
fmt.Printf("%4s <- rf(%d)\n", srfact, fact) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 760: | Line 760: | ||
some of the specific tasks (some of the hyperfactorials and the computation of 5$) |
some of the specific tasks (some of the hyperfactorials and the computation of 5$) |
||
but can otherwise be used. |
but can otherwise be used. |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# for integer precision: |
# for integer precision: |
||
def power($b): . as $a | reduce range(0;$b) as $i (1; . * $a); |
def power($b): . as $a | reduce range(0;$b) as $i (1; . * $a); |
||
Line 806: | Line 806: | ||
.i += 1 |
.i += 1 |
||
| .fact = .fact * .i) |
| .fact = .fact * .i) |
||
| if .fact > $n then null else .i end;</ |
| if .fact > $n then null else .i end;</syntaxhighlight> |
||
''' The tasks:''' |
''' The tasks:''' |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
"First 10 superfactorials:", |
"First 10 superfactorials:", |
||
(range(0;10) | sf), |
(range(0;10) | sf), |
||
Line 825: | Line 825: | ||
"\nReverse factorials:", |
"\nReverse factorials:", |
||
( 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 119 | "\(rf) <- rf(\(.))") |
( 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 119 | "\(rf) <- rf(\(.))") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Invocation: gojq -nr -f special-factorials.jq |
Invocation: gojq -nr -f special-factorials.jq |
||
Line 833: | Line 833: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
No recursion. |
No recursion. |
||
< |
<syntaxhighlight lang="julia">superfactorial(n) = n < 1 ? 1 : mapreduce(factorial, *, 1:n) |
||
sf(n) = superfactorial(n) |
sf(n) = superfactorial(n) |
||
Line 874: | Line 874: | ||
println(rpad(n, 10), rf(n)) |
println(rpad(n, 10), rf(n)) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
N Superfactorial Hyperfactorial Alternating Factorial Exponential Factorial |
N Superfactorial Hyperfactorial Alternating Factorial Exponential Factorial |
||
Line 909: | Line 909: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="scala">import java.math.BigInteger |
||
import java.util.function.Function |
import java.util.function.Function |
||
Line 1,024: | Line 1,024: | ||
testInverse(3628800) |
testInverse(3628800) |
||
testInverse(119) |
testInverse(119) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 10 super factorials: |
<pre>First 10 super factorials: |
||
Line 1,052: | Line 1,052: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="lua">-- n! = 1 * 2 * 3 * ... * n-1 * n |
||
function factorial(n) |
function factorial(n) |
||
local result = 1 |
local result = 1 |
||
Line 1,158: | Line 1,158: | ||
test_inverse(362880) |
test_inverse(362880) |
||
test_inverse(3628800) |
test_inverse(3628800) |
||
test_inverse(119)</ |
test_inverse(119)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 9 super factorials |
<pre>First 9 super factorials |
||
Line 1,185: | Line 1,185: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[sf, expf] |
||
sf[n_] := BarnesG[2 + n] |
sf[n_] := BarnesG[2 + n] |
||
expf[n_] := Power @@ Range[n, 1, -1] |
expf[n_] := Power @@ Range[n, 1, -1] |
||
Line 1,192: | Line 1,192: | ||
Hyperfactorial /@ Range[0, 9] |
Hyperfactorial /@ Range[0, 9] |
||
AlternatingFactorial /@ Range[0, 9] |
AlternatingFactorial /@ Range[0, 9] |
||
expf /@ Range[0, 4]</ |
expf /@ Range[0, 4]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{1, 1, 2, 12, 288, 34560, 24883200, 125411328000, 5056584744960000, 1834933472251084800000} |
<pre>{1, 1, 2, 12, 288, 34560, 24883200, 125411328000, 5056584744960000, 1834933472251084800000} |
||
Line 1,201: | Line 1,201: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<syntaxhighlight lang="nim">import math, strformat, strutils, sugar |
||
import bignum |
import bignum |
||
Line 1,265: | Line 1,265: | ||
for n in [1, 2, 6, 24, 119, 120, 720, 5040, 40320, 362880, 3628800]: |
for n in [1, 2, 6, 24, 119, 120, 720, 5040, 40320, 362880, 3628800]: |
||
let r = rf(n) |
let r = rf(n) |
||
echo &"{n:7}: ", if r >= 0: &"{r:2}" else: "undefined"</ |
echo &"{n:7}: ", if r >= 0: &"{r:2}" else: "undefined"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,290: | Line 1,290: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature qw<signatures say>; |
use feature qw<signatures say>; |
||
Line 1,313: | Line 1,313: | ||
say '5$ has ' . length(5**4**3**2) . ' digits'; |
say '5$ has ' . length(5**4**3**2) . ' digits'; |
||
say 'rf : ' . join ' ', map { rf $_ } <1 2 6 24 120 720 5040 40320 362880 3628800>; |
say 'rf : ' . join ' ', map { rf $_ } <1 2 6 24 120 720 5040 40320 362880 3628800>; |
||
say 'rf(119) = ' . rf(119);</ |
say 'rf(119) = ' . rf(119);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>sf : 1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000 |
<pre>sf : 1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000 |
||
Line 1,328: | Line 1,328: | ||
which means sf/H/af/ef aren't usable independently as-is, but reconstitution should be easy enough |
which means sf/H/af/ef aren't usable independently as-is, but reconstitution should be easy enough |
||
(along with somewhat saner and thread-safe routine-local vars). |
(along with somewhat saner and thread-safe routine-local vars). |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 1,388: | Line 1,388: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Reverse factorials: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">120</span><span style="color: #0000FF;">,</span><span style="color: #000000;">720</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5040</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40320</span><span style="color: #0000FF;">,</span><span style="color: #000000;">362880</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3628800</span><span style="color: #0000FF;">,</span><span style="color: #000000;">119</span><span style="color: #0000FF;">},</span><span style="color: #000000;">rf</span><span style="color: #0000FF;">))})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Reverse factorials: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">,</span><span style="color: #000000;">120</span><span style="color: #0000FF;">,</span><span style="color: #000000;">720</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5040</span><span style="color: #0000FF;">,</span><span style="color: #000000;">40320</span><span style="color: #0000FF;">,</span><span style="color: #000000;">362880</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3628800</span><span style="color: #0000FF;">,</span><span style="color: #000000;">119</span><span style="color: #0000FF;">},</span><span style="color: #000000;">rf</span><span style="color: #0000FF;">))})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,403: | Line 1,403: | ||
It also means less code :) |
It also means less code :) |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
#Aamrun, 5th October 2021 |
#Aamrun, 5th October 2021 |
||
Line 1,452: | Line 1,452: | ||
print("\nrf(119) : " + inverseFactorial(119)) |
print("\nrf(119) : " + inverseFactorial(119)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,475: | Line 1,475: | ||
</pre> |
</pre> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub postfix:<!> ($n) { [*] 1 .. $n } |
||
sub postfix:<$> ($n) { [R**] 1 .. $n } |
sub postfix:<$> ($n) { [R**] 1 .. $n } |
||
Line 1,494: | Line 1,494: | ||
say 'rf : ', map &rf, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800; |
say 'rf : ', map &rf, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800; |
||
say 'rf(119) = ', rf(119).raku;</ |
say 'rf(119) = ', rf(119).raku;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,506: | Line 1,506: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program to compute some special factorials: superfactorials, hyperfactorials,*/ |
||
/*───────────────────────────────────── alternating factorials, exponential factorials.*/ |
/*───────────────────────────────────── alternating factorials, exponential factorials.*/ |
||
numeric digits 1000 /*allows humongous results to be shown.*/ |
numeric digits 1000 /*allows humongous results to be shown.*/ |
||
Line 1,532: | Line 1,532: | ||
rfr: if x==f then return #; ?= 'undefined'; return ? |
rfr: if x==f then return #; ?= 'undefined'; return ? |
||
hdr: parse arg ?,,$; say 'the first ten '?"factorials:"; return |
hdr: parse arg ?,,$; say 'the first ten '?"factorials:"; return |
||
tell: say substr($, 2); say; $=; return</ |
tell: say substr($, 2); say; $=; return</syntaxhighlight> |
||
{{out|output|text= when using the internal default input:}} |
{{out|output|text= when using the internal default input:}} |
||
<pre> |
<pre> |
||
Line 1,564: | Line 1,564: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "bigint.s7i"; |
include "bigint.s7i"; |
||
Line 1,657: | Line 1,657: | ||
writeln("invFactorial(" <& n <& ") = " <& invFactorial(n)); |
writeln("invFactorial(" <& n <& ") = " <& invFactorial(n)); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,686: | Line 1,686: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func sf(n) { 1..n -> prod {|k| k! } } |
||
func H(n) { 1..n -> prod {|k| k**k } } |
func H(n) { 1..n -> prod {|k| k**k } } |
||
func af(n) { 1..n -> sum {|k| (-1)**(n-k) * k! } } |
func af(n) { 1..n -> sum {|k| (-1)**(n-k) * k! } } |
||
Line 1,738: | Line 1,738: | ||
say ('rf : ', [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800].map(rf)) |
say ('rf : ', [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800].map(rf)) |
||
say ('rf(119) = ', rf(119) \\ 'nil') |
say ('rf(119) = ', rf(119) \\ 'nil') |
||
say ('rf is defined for: ', 8.by { defined(rf(_)) }.join(', ' ) + ', ...')</ |
say ('rf is defined for: ', 8.by { defined(rf(_)) }.join(', ' ) + ', ...')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,755: | Line 1,755: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
We've little choice but to use BigInt here as Wren can only deal natively with integers up to 2^53. |
We've little choice but to use BigInt here as Wren can only deal natively with integers up to 2^53. |
||
< |
<syntaxhighlight lang="ecmascript">import "/big" for BigInt |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,823: | Line 1,823: | ||
System.print("Reverse factorials:") |
System.print("Reverse factorials:") |
||
var facts = [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 119] |
var facts = [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 119] |
||
for (fact in facts) Fmt.print("$4s <- rf($d)", rf.call(fact), fact)</ |
for (fact in facts) Fmt.print("$4s <- rf($d)", rf.call(fact), fact)</syntaxhighlight> |
||
{{out}} |
{{out}} |