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ACpHO,SAAS,0CAAW,CAAS,EAAE,CAAY;IAChD,OAAO,IAAI,EAAE,IAAI,OAAO,EAAE;AAC5B;AAWO,SAAS,0CAAW,KAAyB;IAClD,6BAA6B;IAC7B,IAAI,GAAc;IAClB,IAAI,OAAO,UAAU,UAAU;QAC7B,IAAI;QACJ,IAAI,CAAA,GAAA,yCAAK,EAAE;IACb,OAAO;QACL,IAAI;QACJ,IAAI,CAAA,GAAA,yCAAO,EAAE;IACf;IAEA,0BAA0B;IAC1B,IAAI,CAAA,GAAA,yCAAO,EAAE,IAAI;QACf,IAAI,MAAM;QACV,MAAO,IAAI,EAAE,CAAE;YACb,MAAM,EAAE;YACR;QACF;QACA,OAAO;YAAC;SAAI;IACd,OACE,OAAO,CAAA,GAAA,yCAAG,EAAE,GAAG,0CAAW,GAAG;AAEjC;;;;;AN1CO,MAAM,2CAAU;SACrB;YACA;YACA;qBACA;cACA;AACF;AAGO,MAAM,2CAAY;mBACvB;mBACA;AACF;AAGO,MAAM,4CAAU;gBACrB;uBACA;IACA,YAAY,CAAA,GAAA,yCAAI;AAClB;AAGO,MAAM,4CAAU;gBACrB;IACA,YAAY,CAAA,GAAA,yCAAI;AAClB;AAGO,MAAM,4CAAO;gBAClB;cACA;cACA;cACA;YACA;AACF;AAGO,MAAM,4CAAS;SACpB;UACA;aACA;gBACA;cACA;AACF","sources":["src/index.ts","src/collatz.ts","src/iterative.ts","src/prefix.ts","src/util.ts","src/piptree.ts","src/riptree.ts"],"sourcesContent":["import {ecf, ecfToN, length, reducedSequence, sequence} from './collatz';\nexport const collatz = {\n  ecf,\n  ecfToN,\n  length,\n  reducedSequence,\n  sequence,\n} as const;\n\nimport {pathExtension, prefixIterate} from './iterative';\nexport const iterative = {\n  pathExtension,\n  prefixIterate,\n} as const;\n\nimport {findNature, getRootDirections, prefixFind as pippf} from './piptree';\nexport const piptree = {\n  findNature,\n  getRootDirections,\n  prefixFind: pippf,\n} as const;\n\nimport {nextInPath, prefixFind as rippf} from './riptree';\nexport const riptree = {\n  nextInPath,\n  prefixFind: rippf,\n} as const;\n\nimport {fromBinary, fromPath, isPower2, toBinary, toPath} from './util';\nexport const util = {\n  fromBinary,\n  fromPath,\n  isPower2,\n  toBinary,\n  toPath,\n} as const;\n\nimport {add, find, iterate, mapFromNum, mapToNum} from './prefix';\nexport const prefix = {\n  add,\n  find,\n  iterate,\n  mapFromNum,\n  mapToNum,\n} as const;\n","/** Collatz length is the number of iterations it takes to reach n to 1. */\nexport function length(n: bigint): number {\n  let ans = 0;\n  while (n > 1n) {\n    ans++;\n    if ((n & 1n) === 0n) {\n      n >>= 1n;\n    } else {\n      n = 3n * n + 1n;\n    }\n  }\n  return ans;\n}\n\n/** Collatz Sequence is the array of numbers seen during iterations until 1 is reached. */\nexport function sequence(n: bigint): bigint[] {\n  const ans: bigint[] = [];\n  while (n > 1n) {\n    ans.push(n);\n    if ((n & 1n) === 0n) {\n      n >>= 1n;\n    } else {\n      n = 3n * n + 1n;\n    }\n  }\n  ans.push(1n);\n  return ans;\n}\n\n/** Reduced Collatz Sequence is the array of odd numbers seen during iterations until 1 is reached */\nexport function reducedSequence(n: bigint): bigint[] {\n  const ans: bigint[] = [];\n  if ((n & 1n) === 0n) ans.push(n);\n  while (n > 1n) {\n    if ((n & 1n) === 0n) {\n      n >>= 1n;\n    } else {\n      ans.push(n);\n      n = 3n * n + 1n;\n    }\n  }\n  ans.push(1n);\n  return ans;\n}\n\n/** Find **ECF** (Exponential Canonical Form) of a number. */\nexport function ecf(n: bigint): number[] {\n  let twos = 0;\n  const ans: number[] = [];\n  while (n > 1n) {\n    if ((n & 1n) === 0n) {\n      twos++;\n      n >>= 1n;\n    } else {\n      ans.push(twos);\n      n = 3n * n + 1n;\n    }\n  }\n  ans.push(twos);\n  return ans;\n}\n\n/** Compute a number from it's ECF. */\nexport function ecfToN(ecf: number[]): bigint {\n  let ans = 1n;\n  for (let i = ecf.length - 1; i > 0; --i) {\n    ans = ans << BigInt(ecf[i] - ecf[i - 1]);\n    ans = (ans - 1n) / 3n;\n  }\n  ans = ans << BigInt(ecf[0]);\n  return ans;\n}\n","import {add, iterate} from './prefix';\nimport {toPath} from './util';\n\nexport function pathExtension(n: bigint, prefixFinder: (p: boolean[]) => number[]): number[] {\n  const p: boolean[] = toPath(n);\n  let pf: number[] = prefixFinder(p);\n  while (iterate(n, pf) !== 1n) {\n    p.push(true);\n    pf = prefixFinder(p);\n  }\n  return pf;\n}\n\nexport function prefixIterate(n: bigint, prefixFinder: (n: bigint) => number[]): number[] {\n  let ans: number[] = [];\n  let pf: number[] = [];\n\n  // eslint-disable-next-line no-constant-condition\n  while (true) {\n    pf = prefixFinder(n);\n    ans = add(ans, pf);\n    n = iterate(n, pf);\n    if (n === 1n) {\n      return ans;\n    } else {\n      n = 3n * n + 1n;\n      if (ans.length !== 0) {\n        ans.push(ans[ans.length - 1]);\n      }\n    }\n  }\n}\n","import {ecf} from './collatz';\n\n/**\n * Returns the prefix of two numbers.\n *\n * The prefix can be thought of as common prefix of the ECFs.\n * As an example, ECF(3) = [0, 1, 5] and ECF(7) = [0, 1, 2, 4, 7, 11].\n * The common prefix here is [0, 1], thus prefix(3,7) = prefix(7,3) = [0, 1].\n */\nexport function find(n: bigint, m: bigint): number[] {\n  if (n === m) return ecf(n);\n  const ans: number[] = [];\n  let twos = 0;\n  // eslint-disable-next-line no-constant-condition\n  while (true) {\n    if ((n & 1n) === 0n && (m & 1n) === 0n) {\n      // both are even\n      twos++;\n      n = n >> 1n;\n      m = m >> 1n;\n    } else if ((n & 1n) === 1n && (m & 1n) === 1n) {\n      // both are odd\n      ans.push(twos);\n      n = 3n * n + 1n;\n      m = 3n * m + 1n;\n    } else break; // different parity\n  }\n  return ans;\n}\n\n/**\n * Iterates a number through a prefix.\n *\n * If the prefix is equal to ECF of the number,\n * the result is expected to be 1.\n */\nexport function iterate(n: bigint, pf: number[]): bigint {\n  if (pf.length === 0) return n;\n  n = n / 2n ** BigInt(pf[0]);\n  for (let i = 1; i < pf.length; ++i) {\n    n = 3n * n + 1n;\n    n = n / 2n ** BigInt(pf[i] - pf[i - 1]);\n  }\n  return n;\n}\n\n/** Bijective mapping from a prefix to a number. */\nexport function mapToNum(pf: number[]): bigint {\n  return pf.map(BigInt).reduce((acc, p) => acc + 2n ** p, 0n);\n}\n\n/** Bijective mapping from a number to a prefix. */\nexport function mapFromNum(n: bigint): number[] {\n  const ans: number[] = [];\n  for (let bitPos = 0; n > 0; bitPos++) {\n    if ((n & 1n) === 1n) {\n      ans.push(bitPos);\n    }\n    n >>= 1n;\n  }\n  return ans;\n}\n\n/** Add two prefixes. */\nexport function add(pf1: number[], pf2: number[]): number[] {\n  if (pf1.length === 0) {\n    return pf2;\n  } else if (pf2.length === 0) {\n    return pf1;\n  } else {\n    const pf1last = pf1.pop()!; // ! to show it cant be null\n    pf2 = pf2.map(v => v + pf1last);\n    pf1 = pf1.concat(pf2);\n    return pf1;\n  }\n}\n","/**\n * Finds the first number that resides at the given path.\n * @param p path\n * @returns number\n */\nexport function fromPath(p: boolean[]): bigint {\n  p = p.map(b => !b); // flip\n  p.reverse(); // reverse\n  let n = fromBinary(p); // to number\n  n++; // increment\n  return n;\n}\n\n/**\n * Obtain the shortest path to given number.\n * @param n number\n * @returns path\n */\nexport function toPath(n: bigint): boolean[] {\n  n--; // decrement\n  let p = toBinary(n); // from binary\n  p.reverse(); // reverse\n  p = p.map(b => !b); // flip\n  return p;\n}\n\n/**\n * Find the number from its binary representation.\n * @param b binary representation\n * @returns number\n */\nexport function fromBinary(b: boolean[]): bigint {\n  let ans = 0n;\n  b.forEach(isSet => {\n    ans <<= 1n;\n    if (isSet) ans++;\n  });\n  return ans;\n}\n\n/**\n * Obtain the binary representation of a number\n * @param n number\n * @returns binary representation\n */\nexport function toBinary(n: bigint): boolean[] {\n  const ans: boolean[] = [];\n  while (n !== 0n) {\n    ans.unshift((n & 1n) === 1n);\n    n >>= 1n;\n  }\n  return ans;\n}\n\n/**\n * Find if a given number is a power of two\n * @param n number\n * @returns `true` if number is a power of two\n */\nexport function isPower2(n: bigint): boolean {\n  if (n === 0n) {\n    return true;\n  } else {\n    return (n & (n - 1n)) === 0n;\n  }\n}\n","import {iterate} from './prefix';\nimport {fromPath, fromBinary, isPower2, toPath} from './util';\n\n/**\n * Finds the nature of a path.\n *\n * @param p path\n * @param pf prefix\n * @param rpf root prefix\n * @returns nature\n */\nexport function findNature(p: boolean[], pf: number[], rpf: number): boolean {\n  const n: bigint = fromPath(p);\n  const iter_res: bigint = iterate(n, pf.concat(rpf + 1));\n  return (iter_res & 1n) === 0n;\n}\n\n/**\n * Finds the path from root to the node indexed by p in PIPTree, with the path length of the root node being equal to |p|.\n *\n * It starts from the target, and in a loop either does `m/2` or `(m-1)/2` until it reaches 1.\n * This gives the path from that number to root, so we reverse that to obtain the road from path to target.\n *\n * NOTE: There could be a faster way to do this by just looking at the path. The number of 0s gives you the level. The direction can be read from the binary representation, [0, 1, 1] is like [R],\n * [1, 1, 1] is [R, R], [1, 0, 1] is [L, R] etc.\n *\n * @param p path\n * @returns list of directions\n */\nexport function getRootDirections(p: boolean[]): boolean[] {\n  const ans: boolean[] = [];\n  let i: bigint = fromBinary(p);\n  while (i > 1n) {\n    if ((i & 1n) === 0n) {\n      i = i >> 1n;\n      ans.push(false); // left\n    } else {\n      i = (i - 1n) >> 1n;\n      ans.push(true); // right\n    }\n  }\n  ans.reverse();\n  return ans;\n}\n\n/**\n * Finds the prefix of a number using PIPTree properties.\n * @param input an integer or a path\n * @returns prefix\n */\nexport function prefixFind(input: bigint | boolean[]): number[] {\n  // typechecking and assigning\n  let p: boolean[], n: bigint;\n  if (typeof input === 'bigint') {\n    n = input;\n    p = toPath(n);\n  } else {\n    p = input;\n    n = fromPath(p);\n  }\n\n  // edge case: power of two\n  if (isPower2(n)) {\n    let ans = 0;\n    while (n > 1n) {\n      n >>= 1n;\n      ans++;\n    }\n    return [ans];\n  }\n\n  // find directions from root to p\n  const dir: boolean[] = getRootDirections(p);\n\n  // calculate the root prefix\n  const rpf: number = p.length - 1;\n\n  // calculate the root number\n  const r: bigint = 1n << BigInt(rpf);\n\n  // calculate the root path [0, 0, ..., 0, 1]\n  const rp = Array.from({length: p.length}).fill(false);\n  rp[rp.length - 1] = true;\n\n  // start from the root and work your way to the target\n  let cur_n: bigint = r;\n  let cur_p: boolean[] = JSON.parse(JSON.stringify(rp));\n  let cur_pf: number[] = [rpf];\n\n  dir.forEach(d => {\n    // find nature of current node\n    const nat: boolean = findNature(cur_p, cur_pf, rpf);\n\n    // minus 1 everything in the prefix\n    cur_pf = cur_pf.map(x => x - 1);\n\n    if (d) {\n      // if BAD and RIGHT, append root prefix\n      if (nat === false) {\n        cur_pf.push(rpf);\n      }\n\n      // div 2\n      cur_n = cur_n >> 1n;\n\n      // go to the right child\n      cur_p.push(true);\n      cur_p = cur_p.slice(1);\n    } else {\n      // if GOOD and LEFT, append root prefix\n      if (nat === true) {\n        cur_pf.push(rpf);\n      }\n\n      // div 2 and plus root\n      cur_n = (cur_n >> 1n) + r;\n\n      // go to the left child\n      cur_p.push(false);\n      cur_p = cur_p.slice(1);\n    }\n  });\n\n  return cur_pf;\n}\n","import {find} from './prefix';\nimport {toPath, fromPath, isPower2} from './util';\n\n/**\n * Finds the next number that resides at the path of `n`.\n *\n * The path is also given, as `n` can be in different paths (see path extension)\n */\nexport function nextInPath(n: bigint, p: boolean[]): bigint {\n  return n + 2n ** BigInt(p.length);\n}\n\n/**\n * Finds the prefix of a number, or a number at the given path.\n *\n * We obtain the next number in that path with n + 2^|p|.\n * Then we look at the prefix of these two numbers.\n *\n * Note that sometimes the prefix of one number might fall short because the other one reached 1.\n * However, we can extend such cases by looping 1 -> 1 and extending the respectiveECF.\n */\nexport function prefixFind(input: bigint | boolean[]): number[] {\n  // typechecking and assigning\n  let p: boolean[], n: bigint;\n  if (typeof input === 'bigint') {\n    n = input;\n    p = toPath(n);\n  } else {\n    p = input;\n    n = fromPath(p);\n  }\n\n  // edge case: power of two\n  if (isPower2(n)) {\n    let ans = 0;\n    while (n > 1n) {\n      n >>= 1n;\n      ans++;\n    }\n    return [ans];\n  } else {\n    return find(n, nextInPath(n, p));\n  }\n}\n"],"names":[],"version":3,"file":"index.mjs.map","sourceRoot":"../"}