Special factorials: Difference between revisions

Line 745:
119 nothing
</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<lang scala>import java.math.BigInteger
import java.util.function.Function
 
/* n! = 1 * 2 * 3 * ... * n */
fun factorial(n: Int): BigInteger {
val bn = BigInteger.valueOf(n.toLong())
var result = BigInteger.ONE
var i = BigInteger.TWO
while (i <= bn) {
result *= i++
}
return result
}
 
/* if(n!) = n */
fun inverseFactorial(f: BigInteger): Int {
if (f == BigInteger.ONE) {
return 0
}
 
var p = BigInteger.ONE
var i = BigInteger.ONE
 
while (p < f) {
p *= i++
}
 
if (p == f) {
return i.toInt() - 1
}
return -1
}
 
/* sf(n) = 1! * 2! * 3! * ... . n! */
fun superFactorial(n: Int): BigInteger {
var result = BigInteger.ONE
for (i in 1..n) {
result *= factorial(i)
}
return result
}
 
/* H(n) = 1^1 * 2^2 * 3^3 * ... * n^n */
fun hyperFactorial(n: Int): BigInteger {
var result = BigInteger.ONE
for (i in 1..n) {
val bi = BigInteger.valueOf(i.toLong())
result *= bi.pow(i)
}
return result
}
 
/* af(n) = -1^(n-1)*1! + -1^(n-2)*2! + ... + -1^(0)*n! */
fun alternatingFactorial(n: Int): BigInteger {
var result = BigInteger.ZERO
for (i in 1..n) {
if ((n - i) % 2 == 0) {
result += factorial(i)
} else {
result -= factorial(i)
}
}
return result
}
 
/* n$ = n ^ (n - 1) ^ ... ^ (2) ^ 1 */
fun exponentialFactorial(n: Int): BigInteger {
var result = BigInteger.ZERO
for (i in 1..n) {
result = BigInteger.valueOf(i.toLong()).pow(result.toInt())
}
return result
}
 
fun testFactorial(count: Int, f: Function<Int, BigInteger>, name: String) {
println("First $count $name:")
for (i in 0 until count) {
print("${f.apply(i)} ")
}
println()
}
 
fun testInverse(f: Long) {
val n = inverseFactorial(BigInteger.valueOf(f))
if (n < 0) {
println("rf($f) = No Solution")
} else {
println("rf($f) = $n")
}
}
 
fun main() {
testFactorial(10, ::superFactorial, "super factorials")
println()
 
testFactorial(10, ::hyperFactorial, "hyper factorials")
println()
 
testFactorial(10, ::alternatingFactorial, "alternating factorials")
println()
 
testFactorial(5, ::exponentialFactorial, "exponential factorials")
println()
 
testInverse(1)
testInverse(2)
testInverse(6)
testInverse(24)
testInverse(120)
testInverse(720)
testInverse(5040)
testInverse(40320)
testInverse(362880)
testInverse(3628800)
testInverse(119)
}</lang>
{{out}}
<pre>First 10 super factorials:
1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000
 
First 10 hyper factorials:
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:
0 1 2 9 262144
 
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) = No Solution</pre>
 
=={{header|Lua}}==
1,452

edits