Special factorials: Difference between revisions

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

edits