Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

27
LINES

< > BotCompany Repo | #1030378 // Turn Ackermann function into a virtual stack [memory-compact version plus tail calls, shortened]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (2478L/13K).

1  
cprint {
2  
  sclass f_ackermann implements VStack.Computable<BigInt> {
3  
    BigInt a, b;
4  
    int currentStep; // 0 in the beginning, goes up to 1
5  
6  
    *(BigInt *a, BigInt *b) {}
7  
    
8  
    void step(VStack stack, O subComputationResult) {
9  
      if (currentStep == 0) {
10  
        if (a.equals(BigInt.ZERO))
11  
          stack._return(plus(b, BigInt.ONE));
12  
        else if (b.equals(BigInt.ZERO))
13  
          stack.tailCall(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE);
14  
        else {
15  
          stack.push(new f_ackermann(a, minus(b, BigInt.ONE)));
16  
          currentStep = 1;
17  
        }
18  
      } else if (currentStep == 1)
19  
        stack.tailCall(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt));
20  
      else fail();
21  
    }
22  
  }
23  
  
24  
  start-thread {
25  
    print("Result: " + vStackComputeWithStats(new f_ackermann(bigInt(2), bigInt(2))));
26  
  }
27  
}

Author comment

Began life as a copy of #1030375

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030378
Snippet name: Turn Ackermann function into a virtual stack [memory-compact version plus tail calls, shortened]
Eternal ID of this version: #1030378/3
Text MD5: 0a9acb1aa8265df8328984ee713acedb
Transpilation MD5: d94c96a5f578d3286a3cc2c8a86f6e9d
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 14:15:52
Source code size: 873 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 192 / 279
Version history: 2 change(s)
Referenced in: [show references]