Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
33
Task/Ackermann-function/Kotlin/ackermann-function.kotlin
Normal file
33
Task/Ackermann-function/Kotlin/ackermann-function.kotlin
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package ackermann
|
||||
|
||||
fun A(m: Long, n: Long): Long = when {
|
||||
m == 0L -> n + 1
|
||||
m > 0L -> when {
|
||||
n == 0L -> A(m - 1, 1)
|
||||
n > 0L -> A(m - 1, A(m, n - 1))
|
||||
else -> throw IllegalArgumentException("illegal n")
|
||||
}
|
||||
else -> throw IllegalArgumentException("illegal m")
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val M: Long = 4
|
||||
val N: Long = 20
|
||||
val r = 0..N
|
||||
for (m in 0..M) {
|
||||
print("\nA(%d, %s) =".format(m, r))
|
||||
var able = true
|
||||
r forEach {
|
||||
try {
|
||||
if (able) {
|
||||
val a = A(m, it)
|
||||
print(" %6d".format(a))
|
||||
} else
|
||||
print(" %6s".format("?"))
|
||||
} catch(e: Throwable) {
|
||||
print(" %6s".format("?"))
|
||||
able = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue