ApasScript.

Number Theory

factorial

n! -- the product of every whole number from 1 to n.

nCr

The number of ways to choose r items from n, order not mattering (n choose r).

nPr

The number of ways to arrange r items from n, order mattering (n permute r).

catalan

The nth Catalan number -- counts things like balanced bracket sequences and binary tree shapes.

fibonacci

The nth Fibonacci number (0, 1, 1, 2, 3, 5, 8, ...).

gcd

The greatest common divisor of a and b. Works at any size (past 10^38 through big integers).

lcm

The least common multiple of a and b. Works at any size (past 10^38 through big integers).

divides

True if a evenly divides b (b is a multiple of a).

quotient

The quotient q of the division algorithm a = b*q + r with 0 <= r < |b|, exact at any size. For a positive b it is floor(a/b); quotient(-17, 5) is -4 and quotient(17, -5) is -3.

remainder

The remainder r of the division algorithm a = b*q + r with 0 <= r < |b|: never negative, exact at any size. The same as a % b when b is positive; remainder(-17, 5) is 3 and remainder(17, -5) is 2 (17 % -5 is -3).

divmod

Quotient and remainder together, as {quotient, remainder}, from the division algorithm a = b*q + r with 0 <= r < |b|. Read them with r.quotient and r.remainder.

sumDivisors

The sum of all positive divisors of n, including 1 and n itself.

countDivisors

How many positive divisors n has, including 1 and n itself.

mulmod

(a * b) mod m, after first reducing a and b modulo m so their full product is never formed. Works at any size (past 10^38 through big integers).

modPow

base^exp mod m, computed efficiently (fast exponentiation) without ever forming the full power. Works at any size (past 10^38 through big integers).

modInv

The modular inverse of a mod m -- the value x where (a*x) mod m == 1. Returns explanatory text ('no inverse exists') when a and m share a common factor.

solveCongruence

Finds one solution to the linear congruence a*x = b (mod m). Returns explanatory text ('no solution exists') when none exists.

fermatCheck

Checks whether a^(p-1) = 1 (mod p). A false result proves p is composite; a true result does not prove p is prime.

isPrime

True if n is a prime number. Works at any size (past 10^38 through big integers).

nextPrime

The smallest prime strictly greater than n. Works at any size (past 10^38 through big integers).

triangular

The nth triangular number, 1 + 2 + ... + n. Works at any size (past 10^38 through big integers).

isTriangular

True if n is a triangular number (1, 3, 6, 10, 15, ...).

piNaive

pi(n) -- the count of primes less than or equal to n, computed by direct trial (simple, fine for small n).

piSieve

pi(n) -- the count of primes less than or equal to n, computed via a sieve (fast for larger n).

nthPrime

The nth prime number (nthPrime(1) = 2, nthPrime(2) = 3, ...).

isSquareFree

True if no prime's square divides n -- n has no repeated prime factor.

isPerfectPower

True if positive integer n can be written as m^k for integers m and k >= 2 (e.g. 8 = 2^3, 9 = 3^2).

extendedGcd

x, y and gcd(a, b) with a*x + b*y = gcd(a, b) (extended Euclidean algorithm).

twinPrimes

All twin prime pairs (p, p + 2) up to n.

cousinPrimes

All cousin prime pairs (p, p + 4), both prime, up to n.

sexyPrimes

All sexy prime pairs (p, p + 6), both prime, up to n.

primePairs

All pairs (p, p + d) with both prime, up to n.

consecutivePrimePairs

Consecutive primes exactly gap apart, up to n (prime gaps).

maxPrimeGap

The largest gap between consecutive primes up to n.

isMersennePrime

True if 2^p - 1 is prime (Lucas-Lehmer).

primeWithDigits

The smallest prime with the given number of digits (10^(d-1) rounded up to a prime).

digitCount

The number of decimal digits of a whole number of any size.

toBase

A whole number written in base 2 to 36, as text.

fromBase

The number a base-2..36 string spells.

toHex

A whole number in hexadecimal.

fromHex

The number a hexadecimal string spells.