Special factorials: Difference between revisions

Added Python implementation
(Added Python implementation)
Line 1,316:
</pre>
 
=={{header|Python}}==
Writing a custom factorial instead of math.prod is trivial but using a standard library tool is always a nice change.
 
It also means less code :)
<lang Python>
#Aamrun, 5th October 2021
 
from math import prod
 
def superFactorial(n):
return prod([prod(range(1,i+1)) for i in range(1,n+1)])
 
def hyperFactorial(n):
return prod([i**i for i in range(1,n+1)])
 
def alternatingFactorial(n):
return sum([(-1)**(n-i)*prod(range(1,i+1)) for i in range(1,n+1)])
 
def exponentialFactorial(n):
if n in [0,1]:
return 1
else:
return n**exponentialFactorial(n-1)
def inverseFactorial(n):
i = 1
while True:
if n == prod(range(1,i)):
return i-1
elif n < prod(range(1,i)):
return "undefined"
i+=1
 
print("Superfactorials for [0,9] :")
print({"sf(" + str(i) + ") " : superFactorial(i) for i in range(0,10)})
 
print("\nHyperfactorials for [0,9] :")
print({"H(" + str(i) + ") " : hyperFactorial(i) for i in range(0,10)})
 
print("\nAlternating factorials for [0,9] :")
print({"af(" + str(i) + ") " : alternatingFactorial(i) for i in range(0,10)})
 
print("\nExponential factorials for [0,4] :")
print({str(i) + "$ " : exponentialFactorial(i) for i in range(0,5)})
 
print("\nDigits in 5$ : " , len(str(exponentialFactorial(5))))
 
factorialSet = [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
 
print("\nInverse factorials for " , factorialSet)
print({"rf(" + str(i) + ") ":inverseFactorial(i) for i in factorialSet})
 
print("\nrf(119) : " + inverseFactorial(119))
</lang>
{{out}}
<pre>
Superfactorials for [0,9] :
{'sf(0) ': 1, 'sf(1) ': 1, 'sf(2) ': 2, 'sf(3) ': 12, 'sf(4) ': 288, 'sf(5) ': 34560, 'sf(6) ': 24883200, 'sf(7) ': 125411328000, 'sf(8) ': 5056584744960000, 'sf(9) ': 1834933472251084800000}
 
Hyperfactorials for [0,9] :
{'H(0) ': 1, 'H(1) ': 1, 'H(2) ': 4, 'H(3) ': 108, 'H(4) ': 27648, 'H(5) ': 86400000, 'H(6) ': 4031078400000, 'H(7) ': 3319766398771200000, 'H(8) ': 55696437941726556979200000, 'H(9) ': 21577941222941856209168026828800000}
 
Alternating factorials for [0,9] :
{'af(0) ': 0, 'af(1) ': 1, 'af(2) ': 1, 'af(3) ': 5, 'af(4) ': 19, 'af(5) ': 101, 'af(6) ': 619, 'af(7) ': 4421, 'af(8) ': 35899, 'af(9) ': 326981}
 
Exponential factorials for [0,4] :
{'0$ ': 1, '1$ ': 1, '2$ ': 2, '3$ ': 9, '4$ ': 262144}
 
Digits in 5$ : 183231
 
Inverse factorials for [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
{'rf(1) ': 0, 'rf(2) ': 2, 'rf(6) ': 3, 'rf(24) ': 4, 'rf(120) ': 5, 'rf(720) ': 6, 'rf(5040) ': 7, 'rf(40320) ': 8, 'rf(362880) ': 9, 'rf(3628800) ': 10}
 
rf(119) : undefined
</pre>
=={{header|Raku}}==
<lang perl6>sub postfix:<!> ($n) { [*] 1 .. $n }
503

edits