new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
5
Task/Ackermann-function/Scala/ackermann-function-1.scala
Normal file
5
Task/Ackermann-function/Scala/ackermann-function-1.scala
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def ack(m: BigInt, n: BigInt): BigInt = {
|
||||
if (m==0) n+1
|
||||
else if (n==0) ack(m-1, 1)
|
||||
else ack(m-1, ack(m, n-1))
|
||||
}
|
||||
2
Task/Ackermann-function/Scala/ackermann-function-2.scala
Normal file
2
Task/Ackermann-function/Scala/ackermann-function-2.scala
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
scala> for ( m <- 0 to 3; n <- 0 to 6 ) yield ack(m,n)
|
||||
res0: Seq.Projection[BigInt] = RangeG(1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 8, 3, 5, 7, 9, 11, 13, 15, 5, 13, 29, 61, 125, 253, 509)
|
||||
40
Task/Ackermann-function/Scala/ackermann-function-3.scala
Normal file
40
Task/Ackermann-function/Scala/ackermann-function-3.scala
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
val maxDepth = 4
|
||||
|
||||
val ackMMap = scala.collection.mutable.Map[BigInt, BigInt]()
|
||||
val ackNMaps = Array.fill(maxDepth + 1) { scala.collection.mutable.Map[BigInt, BigInt]() }
|
||||
|
||||
def ack(m: Int, n: BigInt): BigInt = {
|
||||
if ((m < 0) || (n < 0)) {
|
||||
throw new Exception("Negative parameters are not allowed: ack(%s, %s)".format(m, n))
|
||||
}
|
||||
if (m > maxDepth) {
|
||||
throw new Exception("First parameter is greater as %s: ack(%s, %s)".format(maxDepth, m, n))
|
||||
}
|
||||
|
||||
val newM = m - 1
|
||||
val newN = n - 1
|
||||
|
||||
if (m == 0) {
|
||||
n + 1
|
||||
} else if (n == 0) {
|
||||
ackMMap.getOrElseUpdate(newM, ack(newM, 1))
|
||||
} else {
|
||||
val createStep = 125
|
||||
val index = m
|
||||
val mapCurrent = ackNMaps(index)
|
||||
val mapPrevious = ackNMaps(index - 1)
|
||||
val maxRecursion = 2 * createStep
|
||||
val nrOfElements : BigInt = if (mapCurrent.isEmpty) 0 else mapCurrent.max._1
|
||||
|
||||
if ((nrOfElements + maxRecursion) < n) {
|
||||
for (i <- nrOfElements + createStep to n by createStep) {
|
||||
mapCurrent.getOrElseUpdate(i, ack(m, i))
|
||||
}
|
||||
}
|
||||
mapCurrent.getOrElseUpdate(n, {
|
||||
val ackVal = mapCurrent.getOrElseUpdate(newN, ack(m, newN))
|
||||
|
||||
mapPrevious.getOrElseUpdate(ackVal, ack(newM, ackVal))
|
||||
})
|
||||
}
|
||||
}
|
||||
5
Task/Ackermann-function/Scala/ackermann-function-4.scala
Normal file
5
Task/Ackermann-function/Scala/ackermann-function-4.scala
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
if ((nrOfElements + maxRecursion) < n) {
|
||||
for (i <- nrOfElements + createStep to n by createStep) {
|
||||
mapCurrent.getOrElseUpdate(i, ack(m, i))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue