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

16
LINES

< > BotCompany Repo | #1030359 // collatzLength - length of Collatz sequence for a number

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

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

static BigInt collatzLength(long n) {
  ret collatzLength(bigInt(n));
}

static BigInt collatzLength(BigInt n) {
  BigInt one = BigInt.ONE, three = bigInt(3);
  BigInt counter = one;
  while ping (neq(n, one)) {
    counter = plus(counter, one);
    if (even(n.intValue()))
      n = div(n, BigInt.TWO);
    else
      n = plus(mul(n, three), one);
  }
  ret counter;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030359
Snippet name: collatzLength - length of Collatz sequence for a number
Eternal ID of this version: #1030359/1
Text MD5: 67d4186c78ee2901fb58ca9c918cbb1f
Transpilation MD5: 0da204c11a8b07cd584111aa30ab7dc1
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:03:29
Source code size: 384 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 89 / 148
Referenced in: [show references]