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

27
LINES

< > BotCompany Repo | #1030379 // Turn Ackermann function into a virtual stack [even more memory-compact version without currentStep field]

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

Uses 1059K of libraries. Click here for Pure Java version (6138L/31K).

cprint {
  srecord f_ackermann(BigInt a, BigInt b) implements VStack.Computable<BigInt> {
    void step(VStack stack, O subComputationResult) {
      if (!stack.hasSubResult()) {
        if (a.equals(BigInt.ZERO))
          stack._return(plus(b, BigInt.ONE));
        else if (b.equals(BigInt.ZERO))
          stack.tailCall(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE);
        else
          stack.push(new f_ackermann(a, minus(b, BigInt.ONE)));
      } else
        stack.tailCall(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt));
    }
  }
  
  start-thread {
    print("Result: " + vStackComputeWithStats(new f_ackermann(bigInt(2), bigInt(2))));
    
    // detailed run
    VStack stack = new(new f_ackermann(bigInt(2), bigInt(2)));
    printStruct(stack);
    while ping (stack.step()) {
      printStruct(stack);
      stack = restructure(stack); // persist to string and re-create
    }
  }
}

Author comment

Began life as a copy of #1030378

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030379
Snippet name: Turn Ackermann function into a virtual stack [even more memory-compact version without currentStep field]
Eternal ID of this version: #1030379/9
Text MD5: c60903f5e18e3a00b7912c6eba1efe59
Transpilation MD5: f3e461c937b69145c31e2e75c866fba7
Author: stefan
Category: javax / maths
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-18 14:53:03
Source code size: 953 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 205 / 321
Version history: 8 change(s)
Referenced in: [show references]