Uses 911K of libraries. Click here for Pure Java version (2446L/13K).
1 | cprint { |
2 | sclass VStack implements Steppable { |
3 | new L<Computable> stack; |
4 | O latestResult; |
5 | |
6 | void push(Computable computation) { |
7 | stack.add(computation); |
8 | } |
9 | |
10 | public bool step() { |
11 | if (empty(stack)) false; |
12 | last(stack).step(this, latestResult); |
13 | true; |
14 | } |
15 | |
16 | void _return(O value) { |
17 | latestResult = value; |
18 | removeLast(stack); |
19 | } |
20 | } |
21 | |
22 | asclass Computable { |
23 | int currentStep; // 0 in the beginning |
24 | |
25 | abstract void step(VStack stack, O subComputationResult); |
26 | } |
27 | |
28 | sclass f_ackermann extends Computable { |
29 | BigInt a, b; |
30 | |
31 | *(BigInt *a, BigInt *b) {} |
32 | |
33 | void step(VStack stack, O subComputationResult) { |
34 | if (currentStep == 0) { |
35 | if (a.equals(BigInt.ZERO)) |
36 | stack._return(plus(b, BigInt.ONE)); |
37 | else if (b.equals(BigInt.ZERO)) { |
38 | stack.push(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE); |
39 | currentStep = 1; |
40 | } else { |
41 | stack.push(new f_ackermann(a, minus(b, BigInt.ONE))); |
42 | currentStep = 2; |
43 | } |
44 | } else if (currentStep == 1) |
45 | stack._return(subComputationResult); |
46 | else if (currentStep == 2) { |
47 | stack.push(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt)); |
48 | currentStep = 1; |
49 | } else fail(); |
50 | } |
51 | } |
52 | |
53 | start-thread { |
54 | new VStack stack; |
55 | stack.push(new f_ackermann(bigInt(2), bigInt(2))); |
56 | stepAllWithStats(stack); |
57 | print("Result: " + stack.latestResult); |
58 | } |
59 | } |
Began life as a copy of #1030373
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030374 |
Snippet name: | Turn Ackermann function into a virtual stack [most memory-compact version, OK] |
Eternal ID of this version: | #1030374/7 |
Text MD5: | e2439651c89439d7735bcee0fbac078f |
Transpilation MD5: | 98fa6e8394683ca23e63e66c5d8ce7e8 |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-12-10 13:58:44 |
Source code size: | 1554 bytes / 59 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 210 / 317 |
Version history: | 6 change(s) |
Referenced in: | [show references] |