Special factorials: Difference between revisions

Content added Content deleted
(Add Seed7 example)
Line 1,022: Line 1,022:
rf(3628800 = 10
rf(3628800 = 10
rf(119 = No Solution</pre>
rf(119 = No Solution</pre>

=={{header|Nim}}==
{{libheader[bignum}}
<lang Nim>import math, strformat, strutils, sugar
import bignum

proc pow(a: int; n: Int): Int =
## Compute a^n for "n" big integer.
var n = n
var a = newInt(a)
if a > 0:
result = newInt(1)
# Start with Int values for "n".
while not n.isZero:
if (n and 1) != 0:
result *= a
n = n shr 1
a *= a

func sf(n: Natural): Int =
result = newInt(1)
for i in 2..n:
result *= fac(i)

func hf(n: Natural): Int =
result = newInt(1)
for i in 2..n:
result *= pow(i, uint(i))

func af(n: Natural): Int =
result = newInt(0)
var m = (n and 1) shl 1 - 1
for i in 1..n:
result += m * fac(i)
m = -m

func ef(n: Natural): Int =
result = newInt(1)
for k in 2..n:
result = pow(k, result)

func rf(n: int | Int): int =
if n == 1: return 0
result = 1
var p = newInt(1)
while p < n:
inc result
p *= result
if p > n: result = -1

let sfs = collect(newSeq, for n in 0..9: sf(n))
echo &"First {sfs.len} superfactorials: ", sfs.join(" ")

let hfs = collect(newSeq, for n in 0..9: hf(n))
echo &"First {hfs.len} hyperfactorials: ", hfs.join(" ")

let afs = collect(newSeq, for n in 0..9: af(n))
echo &"First {afs.len} alternating factorials: ", afs.join(" ")

let efs = collect(newSeq, for n in 0..4: ef(n))
echo &"First {efs.len} exponential factorials: ", efs.join(" ")

echo "\nNumber of digits of ef(5): ", len($ef(5))

echo "\nReverse factorials:"
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"</lang>

{{out}}
<pre>First 10 superfactorials: 1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000
First 10 hyperfactorials: 1 1 4 108 27648 86400000 4031078400000 3319766398771200000 55696437941726556979200000 21577941222941856209168026828800000
First 10 alternating factorials: 0 1 1 5 19 101 619 4421 35899 326981
First 5 exponential factorials: 1 1 2 9 262144

Number of digits of ef(5): 183231

Reverse factorials:
1: 0
2: 2
6: 3
24: 4
119: undefined
120: 5
720: 6
5040: 7
40320: 8
362880: 9
3628800: 10</pre>


=={{header|Perl}}==
=={{header|Perl}}==