or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdbitwise.mdcomparison.mdconversion.mdindex.mdproperties.md
tile.json

properties.mddocs/

Property Checks and Number Theory

Methods for checking mathematical properties of BigInteger values including parity, sign, divisibility, primality testing, and number theory operations like GCD and LCM.

Capabilities

Parity Checks

Even Check

Check if a number is even.

/**
 * Checks if the number is even
 * @returns True if the number is divisible by 2
 */
isEven(): boolean;

Usage Examples:

const bigInt = require('big-integer');

// Even numbers
bigInt(6).isEven();   // true
bigInt(0).isEven();   // true
bigInt(-4).isEven();  // true

// Odd numbers
bigInt(3).isEven();   // false
bigInt(-7).isEven();  // false

// Large numbers
bigInt("123456789012345678900").isEven();  // true

Odd Check

Check if a number is odd.

/**
 * Checks if the number is odd
 * @returns True if the number is not divisible by 2
 */
isOdd(): boolean;

Usage Examples:

// Odd numbers
bigInt(13).isOdd();   // true
bigInt(-7).isOdd();   // true

// Even numbers
bigInt(40).isOdd();   // false
bigInt(0).isOdd();    // false

// Large numbers
bigInt("123456789012345678901").isOdd();  // true

Sign Checks

Positive Check

Check if a number is positive. Returns false for ±0.

/**
 * Checks if the number is positive
 * Returns false for 0 and -0
 * @returns True if number > 0
 */
isPositive(): boolean;

Usage Examples:

// Positive numbers
bigInt(54).isPositive();   // true
bigInt(1).isPositive();    // true

// Non-positive numbers
bigInt(-1).isPositive();   // false
bigInt(0).isPositive();    // false
bigInt("-0").isPositive(); // false

// Large positive numbers
bigInt("999999999999999").isPositive();  // true

Negative Check

Check if a number is negative. Returns false for ±0.

/**
 * Checks if the number is negative
 * Returns false for 0 and -0
 * @returns True if number < 0
 */
isNegative(): boolean;

Usage Examples:

// Negative numbers
bigInt(-23).isNegative();  // true
bigInt(-1).isNegative();   // true

// Non-negative numbers
bigInt(50).isNegative();   // false
bigInt(0).isNegative();    // false
bigInt("-0").isNegative(); // false

// Large negative numbers
bigInt("-999999999999999").isNegative();  // true

Zero Check

Check if a number is zero (true for both 0 and -0).

/**
 * Checks if the number is zero
 * Returns true for both 0 and -0
 * @returns True if number is zero
 */
isZero(): boolean;

Usage Examples:

// Zero values
bigInt.zero.isZero();   // true
bigInt(0).isZero();     // true
bigInt("-0").isZero();  // true

// Non-zero values
bigInt(50).isZero();    // false
bigInt(-1).isZero();    // false

// After operations
bigInt(5).subtract(5).isZero();  // true

Unit Check

Check if a number is 1 or -1.

/**
 * Checks if the number is 1 or -1
 * @returns True if number is ±1
 */
isUnit(): boolean;

Usage Examples:

// Unit values
bigInt.one.isUnit();      // true
bigInt(1).isUnit();       // true
bigInt(-1).isUnit();      // true
bigInt.minusOne.isUnit(); // true

// Non-unit values
bigInt(5).isUnit();       // false
bigInt(0).isUnit();       // false
bigInt(2).isUnit();       // false

Divisibility

Divisibility Check

Check if a number is divisible by another number.

/**
 * Checks if the number is divisible by another number
 * @param number - Potential divisor
 * @returns True if this is divisible by number (remainder is zero)
 */
isDivisibleBy(number: BigNumber): boolean;

Usage Examples:

// Divisibility checks
bigInt(999).isDivisibleBy(333);  // true (999 / 333 = 3)
bigInt(99).isDivisibleBy(5);     // false (99 / 5 = 19.8)

// Check for even numbers (divisible by 2)
bigInt(100).isDivisibleBy(2);  // true

// Check for multiples
bigInt(1000).isDivisibleBy(100);  // true
bigInt(1234).isDivisibleBy(100);  // false

// Large numbers
bigInt("123456789000").isDivisibleBy(1000);  // true

Primality Testing

Deterministic Prime Check

Check if a number is prime using a deterministic algorithm.

/**
 * Checks if the number is prime using deterministic primality test
 * @param strict - If true, forces GRH-supported lower bound of 2*log(N)^2 (optional)
 * @returns True if the number is prime
 */
isPrime(strict?: boolean): boolean;

Usage Examples:

// Prime numbers
bigInt(2).isPrime();   // true
bigInt(3).isPrime();   // true
bigInt(5).isPrime();   // true
bigInt(17).isPrime();  // true
bigInt(97).isPrime();  // true

// Composite numbers
bigInt(4).isPrime();   // false
bigInt(6).isPrime();   // false
bigInt(100).isPrime(); // false

// Edge cases
bigInt(1).isPrime();   // false (1 is not prime by definition)
bigInt(0).isPrime();   // false
bigInt(-5).isPrime();  // true (checks primality of absolute value)

// Large primes
bigInt("1000000007").isPrime();  // true

// Strict mode for cryptographic applications
bigInt(1009).isPrime(true);  // true (forces GRH-supported bound)

Probabilistic Prime Check

Check if a number is probably prime using the Miller-Rabin test.

/**
 * Checks if the number is probably prime using Miller-Rabin test
 * Non-deterministic unless a custom RNG is provided
 * If composite, declares probably prime with probability at most 4^(-iterations)
 * If prime, always returns true
 * @param iterations - Number of test iterations (default 5)
 * @param rng - Custom random number generator returning [0,1) (optional)
 * @returns True if probably prime
 */
isProbablePrime(iterations?: number, rng?: () => number): boolean;

Usage Examples:

// Probabilistic primality test with default iterations
bigInt(5).isProbablePrime();   // true
bigInt(49).isProbablePrime();  // false (49 = 7 * 7)
bigInt(1729).isProbablePrime(); // false (Ramanujan's number, composite)

// Increase iterations for higher confidence
bigInt(1009).isProbablePrime(10);  // true (more iterations = higher confidence)

// For very large numbers (faster than deterministic isPrime)
const largePrime = bigInt("982451653");
largePrime.isProbablePrime(20);  // true with high confidence

// Deterministic with custom RNG (for testing)
bigInt(1729).isProbablePrime(1, () => 0.1);  // false
bigInt(1729).isProbablePrime(1, () => 0.2);  // true (false positive with bad witness)

// Cryptographic prime testing
const candidate = bigInt("1000000007");
if (candidate.isProbablePrime(25)) {
  console.log("Probably prime with high confidence");
}

Probability of Error:

For composite numbers, the probability of a false positive is at most 4^(-iterations):

  • 1 iteration: ≤ 25% error rate
  • 5 iterations (default): ≤ 0.098% error rate
  • 10 iterations: ≤ 0.00001% error rate
  • 20 iterations: ≤ 0.0000000001% error rate

Number Theory Operations

Greatest Common Divisor

Find the greatest common divisor (GCD) of two numbers.

/**
 * Finds the greatest common divisor of two numbers
 * Uses the Euclidean algorithm
 * @param a - First value
 * @param b - Second value
 * @returns GCD of a and b
 */
bigInt.gcd(a: BigNumber, b: BigNumber): BigInteger;

Usage Examples:

// Simple GCD
bigInt.gcd(42, 56);  // 14

// GCD with one number being multiple of other
bigInt.gcd(100, 25);  // 25

// Coprime numbers (GCD = 1)
bigInt.gcd(17, 19);  // 1

// GCD with zero
bigInt.gcd(0, 5);    // 5
bigInt.gcd(12, 0);   // 12

// Large numbers
bigInt.gcd("123456789", "987654321");  // 9

// Simplifying fractions
const numerator = bigInt(48);
const denominator = bigInt(60);
const divisor = bigInt.gcd(numerator, denominator);  // 12
const simplified = {
  num: numerator.divide(divisor),      // 4
  denom: denominator.divide(divisor)   // 5
};
// 48/60 simplified to 4/5

Least Common Multiple

Find the least common multiple (LCM) of two numbers.

/**
 * Finds the least common multiple of two numbers
 * LCM(a, b) = (a * b) / GCD(a, b)
 * @param a - First value
 * @param b - Second value
 * @returns LCM of a and b
 */
bigInt.lcm(a: BigNumber, b: BigNumber): BigInteger;

Usage Examples:

// Simple LCM
bigInt.lcm(21, 6);  // 42

// LCM with coprime numbers
bigInt.lcm(5, 7);   // 35 (5 * 7)

// LCM with one multiple of other
bigInt.lcm(10, 5);  // 10

// Finding common period/cycle
bigInt.lcm(12, 18);  // 36

// Large numbers
bigInt.lcm("123", "456");  // 18696

Practical Use Cases

Input Validation

/**
 * Validate that a value is a positive integer
 */
function isPositiveInteger(value) {
  return value.isPositive() && value.equals(value.abs());
}

/**
 * Validate that a value is a natural number (positive or zero)
 */
function isNatural(value) {
  return (value.isPositive() || value.isZero());
}

isPositiveInteger(bigInt(5));   // true
isPositiveInteger(bigInt(-5));  // false
isNatural(bigInt(0));           // true

Prime Number Generation

/**
 * Find the next prime number after n
 */
function nextPrime(n) {
  let candidate = n.next();
  if (candidate.isEven()) {
    candidate = candidate.next();  // Make it odd
  }

  while (!candidate.isPrime()) {
    candidate = candidate.add(2);  // Check only odd numbers
  }

  return candidate;
}

nextPrime(bigInt(10));   // 11
nextPrime(bigInt(100));  // 101

/**
 * Generate n prime numbers starting from start
 */
function generatePrimes(start, count) {
  const primes = [];
  let candidate = start;

  while (primes.length < count) {
    if (candidate.isPrime()) {
      primes.push(candidate);
    }
    candidate = candidate.next();
  }

  return primes;
}

generatePrimes(bigInt(2), 10);
// [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Fraction Operations

/**
 * Simplify a fraction using GCD
 */
function simplifyFraction(numerator, denominator) {
  const divisor = bigInt.gcd(numerator, denominator);
  return {
    numerator: numerator.divide(divisor),
    denominator: denominator.divide(divisor)
  };
}

simplifyFraction(bigInt(48), bigInt(60));
// { numerator: 4, denominator: 5 }

/**
 * Add two fractions
 */
function addFractions(n1, d1, n2, d2) {
  const commonDenom = bigInt.lcm(d1, d2);
  const num1 = n1.multiply(commonDenom.divide(d1));
  const num2 = n2.multiply(commonDenom.divide(d2));

  return simplifyFraction(num1.add(num2), commonDenom);
}

addFractions(bigInt(1), bigInt(3), bigInt(1), bigInt(4));
// { numerator: 7, denominator: 12 } representing 7/12

Cycle Detection

/**
 * Find period of a repeating pattern using LCM
 */
function findCombinedPeriod(periods) {
  return periods.reduce((lcm, period) => bigInt.lcm(lcm, period));
}

// Find when multiple cycles align
const cycleA = bigInt(12);  // Repeats every 12 units
const cycleB = bigInt(18);  // Repeats every 18 units
const cycleC = bigInt(20);  // Repeats every 20 units

findCombinedPeriod([cycleA, cycleB, cycleC]);  // 180

Cryptographic Key Validation

/**
 * Validate that a number is suitable for cryptographic use
 */
function isValidCryptoKey(n, minBits) {
  // Must be positive
  if (!n.isPositive()) return false;

  // Must have minimum bit length
  if (n.bitLength().lesser(minBits)) return false;

  // Should be prime (for RSA, etc.)
  if (!n.isProbablePrime(20)) return false;

  return true;
}

const candidate = bigInt("1000000007");
isValidCryptoKey(candidate, bigInt(16));  // Check if valid

Divisibility Rules

/**
 * Check divisibility by common numbers
 */
function checkDivisibility(n) {
  return {
    by2: n.isEven(),
    by3: n.mod(3).isZero(),
    by5: n.mod(5).isZero(),
    by10: n.mod(10).isZero()
  };
}

checkDivisibility(bigInt(150));
// { by2: true, by3: true, by5: true, by10: true }

Performance Notes

  • Parity checks: O(1) - only need to check last digit
  • Sign checks: O(1) - check sign flag
  • Divisibility: O(n) - requires modulo operation
  • isPrime: O(n³) for deterministic test, but very thorough
  • isProbablePrime: O(k × n²) where k is iterations, much faster for large numbers
  • GCD: O(n²) using Euclidean algorithm
  • LCM: O(n²) computed from GCD

For very large numbers (thousands of digits), isProbablePrime is significantly faster than isPrime while providing strong probabilistic guarantees with sufficient iterations.