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

59
LINES

< > BotCompany Repo | #1030374 // Turn Ackermann function into a virtual stack [most memory-compact version, OK]

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

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

cprint {
  sclass VStack implements Steppable {
    new L<Computable> stack;
    O latestResult;
    
    void push(Computable computation) {
      stack.add(computation);
    }
    
    public bool step() {
      if (empty(stack)) false;
      last(stack).step(this, latestResult);
      true;
    }
    
    void _return(O value) {
      latestResult = value;
      removeLast(stack);
    }
  }
  
  asclass Computable {
    int currentStep; // 0 in the beginning
    
    abstract void step(VStack stack, O subComputationResult);
  }
  
  sclass f_ackermann extends Computable {
    BigInt a, b;

    *(BigInt *a, BigInt *b) {}
    
    void step(VStack stack, O subComputationResult) {
      if (currentStep == 0) {
        if (a.equals(BigInt.ZERO))
          stack._return(plus(b, BigInt.ONE));
        else if (b.equals(BigInt.ZERO)) {
          stack.push(new f_ackermann(a.subtract(BigInt.ONE), BigInt.ONE);
          currentStep = 1;
        } else {
          stack.push(new f_ackermann(a, minus(b, BigInt.ONE)));
          currentStep = 2;
        }
      } else if (currentStep == 1)
        stack._return(subComputationResult);
      else if (currentStep == 2) {
        stack.push(new f_ackermann(minus(a, BigInt.ONE), subComputationResult/BigInt));
        currentStep = 1;
      } else fail();
    }
  }
  
  start-thread {
    new VStack stack;
    stack.push(new f_ackermann(bigInt(2), bigInt(2)));
    stepAllWithStats(stack);
    print("Result: " + stack.latestResult);
  }
}

Author comment

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: 208 / 313
Version history: 6 change(s)
Referenced in: [show references]