number-theory/mod.mjs

/**
 * @module number-theory
 */


const getMod = ZERO => {
  return (dividend, divisor) => {
    if ((dividend < ZERO) ^ (divisor < ZERO)) {
      const res = -dividend % divisor;
      return res === ZERO ? ZERO : divisor - res;
    }
    return dividend % divisor;
  };
};

/**
 * Calculates the modulo of two numbers (Python-like behavior for negative numbers).
 * @param {number} dividend - The dividend.
 * @param {number} divisor - The divisor.
 * @returns {number} The result of the modulo operation.
 * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFAJggvggZjKMBAIgAEBzAawFcBjMEAUxgC8B6auEAGwGdiA3MGBp0QoA|▶ Try it live in Instacode}
 */
export const mod = getMod(0);

/**
 * Calculates the modulo of two BigInts (Python-like behavior for negative numbers).
 * @param {bigint} dividend - The dividend.
 * @param {bigint} divisor - The divisor.
 * @returns {bigint} The result of the modulo operation.
 * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFAJgIQJIIL4IBmMUYCARAAIDmA1gK4DGYIApjAF4D09cIANgGdyAbmDA0WbGKA|▶ Try it live in Instacode}
 */
export const modBI = getMod(0n);