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

23
LINES

< > BotCompany Repo | #1030361 // collatzSequence - Collatz sequence for a number (as iterator). returns final 1 and initial number

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2754L/17K).

static ItIt<BigInt> collatzSequence(long n) {
  ret collatzSequence(bigInt(n));
}

static ItIt<BigInt> collatzSequence(BigInt _n) {
  ret new ItIt<BigInt> {
    BigInt three = bigInt(3);
    BigInt n;
    
    public bool hasNext() {
      ret neq(n, BigInt.ONE);
    }
    
    public BigInt next() {
      if (n == null) ret n = _n; // first number
      if (even(n.intValue()))
        n = div(n, BigInt.TWO);
      else
        n = plus(mul(n, three), BigInt.ONE);
      ret n;
    }
  };
}

Author comment

Began life as a copy of #1030359

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030361
Snippet name: collatzSequence - Collatz sequence for a number (as iterator). returns final 1 and initial number
Eternal ID of this version: #1030361/4
Text MD5: 1e7e86c1ee37560ee7ca9cca99940d32
Transpilation MD5: 1e3078bfd9392994c31d0155bdb21f96
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-09 15:12:00
Source code size: 516 bytes / 23 lines
Pitched / IR pitched: No / No
Views / Downloads: 118 / 171
Version history: 3 change(s)
Referenced in: [show references]