UNPKG

@skenvy/collatz

Version:

Functions related to the Collatz/Syracuse/3N+1 problem, implemented in JavaScript.

36 lines (35 loc) 1.5 kB
import { assertSaneParameterisation } from './FailedSaneParameterCheck.js'; /** * Parameterised Collatz Function * @param parameterisedInputs - Allows non-default (P,a,b) * @returns the output of a single application of a Collatz-esque function. * @throws FailedSaneParameterCheck * Thrown if either P or a are 0. */ export function collatzFunction({ n, P = 2n, a = 3n, b = 1n }) { assertSaneParameterisation(P, a, b); return n % P === 0n ? n / P : (a * n + b); } /** * Parameterised Collatz Inverse Function * @param parameterisedInputs - Allows non-default (P,a,b) * @returns the output of a single application of a Collatz-esque reverse function. * @throws FailedSaneParameterCheck * Thrown if either P or a are 0. */ export function reverseFunction({ n, P = 2n, a = 3n, b = 1n }) { assertSaneParameterisation(P, a, b); // Every input can be reversed as the result of "n/P" division, which yields // "Pn"... {f(n) = an + b}≡{(f(n) - b)/a = n} ~ if n was such that the // muliplication step was taken instead of the division by the modulus, then // (f(n) - b)/a) must be an integer that is not in (0 mod P). Because we're // not placing restrictions on the parameters yet, although there is a better // way of shortcutting this for the default variables, we need to always // attempt (f(n) - b)/a) if ((n - b) % a === 0n && (n - b) % (P * a) !== 0n) { return [(P * n), ((n - b) / a)]; } else { return [P * n]; } }