KT
kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Schundlabua posted a response to:
// https://www.sololearn.com/Discuss/1111022
// Here's an interesting one: The ackermann function.
// It was designed to grow super quickly - wikipedia says A(4,2) has ~20 000 digits - but it always stops and won't get stuck in a loop. It belongs to a nasty breed of recursive function (non-primitive recursive) that can't be represented well without recursion.
// Note: most of this code is an implementation of
// linked list used to keep track of the loop state.
// The ADT class Record maintains state information
// for the ack function as it loops. The A function
// is the recursive version.
// Import and declare random. Debugging counts to
// help with the understanding of the algorithm.
// A loop count for the loop method. A call count
// and maximum nesting level of the recursion.
import java.util.Random
val random = Random()
var loop = 0
var count = 0
var maxLevel = 0
// Get a random number in the range.
fun rand(from: Int, to: Int) = random.nextInt(
to-from+1)+from
// Single node template of the linked list storing
// a value of whatever type, a pointer to the
// previous node, and a pointer to the next node.
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run