ApasScript.

Number Theory

factorial

n! -- the product of every whole number from 1 to n. Works up to n = 1000 (the same for n!).

nCr

The number of ways to choose r items from n, order not mattering (n choose r). Works while k * d^2 is at most 1800000000, where k is the smaller of r and n - r and d the digits of the answer: nCr(3400, 1700) and nCr(10^12, 260) are inside it.

nPr

The number of ways to arrange r items from n, order mattering (n permute r). Works while r * d^2 is at most 5500000000, where d is the digits of the answer: nPr(1650, 825) and nPr(10^12, 330) are inside it.

catalan

The nth Catalan number -- counts things like balanced bracket sequences and binary tree shapes. Works up to n = 1700.

fibonacci

The nth Fibonacci number (0, 1, 1, 2, 3, 5, 8, ...). Works up to n = 4500.

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). Given a list where a number goes, it works item by item (as do modPow and the other functions of whole numbers), and it works inside a rule: g(n) = remainder(n, 4) + 1.

mod

a mod b, the same as a % b: the remainder with the sign of b (floored), so mod(23, 4) is 3 and mod(-7, 3) is 2. A calculator function, so it works in any expression and in a rule; with a list it works item by item.

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).

truthTable

The truth table of a statement in quotes: variables (the letters in alphabetical order), rows (every assignment, all true first), result. Statements read not ! ~, and &, or |, xor, implies ->, iff <->, tightest first; implies groups from the right. Up to 16 letters.

isTautology

Whether a statement in quotes is true under every assignment.

logicallyEquivalent

Whether two statements in quotes have the same value under every assignment.

stirling2

The Stirling number of the second kind S(n, k): ways to split n things into k non-empty groups.

stirling1

The unsigned Stirling number of the first kind c(n, k): permutations of n things with k cycles. The signed number is (-1)^(n-k) times this.

bellNumber

The Bell number B(n): all ways to split n things into groups.

partitionCount

p(n): how many ways n is written as a sum of positive whole numbers, order ignored.

inclusionExclusion

The size of a union from S1, S2, ...: the sums of the sizes of the intersections of 1, 2, ... sets. S1 - S2 + S3 - ...

recurrenceTerm

Term n of a(n) = c1 a(n-1) + ... + ck a(n-k), counted from a(0), exact. Works while n * d^2 is at most 1700000000, where d is n * log10(g) and g the sum of the coefficients' sizes (at least 2; a non-whole coefficient c counts as |c| + 1/|c|): the Fibonacci coefficients reach n = 2650.

solveRecurrence

The closed form in n of the same recurrence, from its characteristic roots, exact, repeated roots included. Refused when a root is not real or cannot be written exactly, or is 0.

eulerPhi

Euler's phi: how many of 1..n are coprime to n, from the prime factorization, so any n of up to 38 digits: eulerPhi(2^61 - 1) is 2^61 - 2.

mobius

The Mobius function: 0 if a square above 1 divides n, otherwise 1 or -1 for an even or odd number of prime factors.

multiplicativeOrder

The order of a mod n: the least k >= 1 with a^k = 1 (mod n). Says so when a and n share a factor, as there is then none.

primitiveRoot

The smallest primitive root of n, or a sentence saying n has none. n up to 10 million.

primitiveRoots

Every primitive root of n, smallest first; an empty list when n has none.

discreteLog

The discrete logarithm, or index: the least x >= 0 with a^x = b (mod n), or a sentence if no power gives b. It goes through the powers in turn, so n is limited to 10 million.

legendreSymbol

The Legendre symbol (a/p) for an odd prime p: 1 if a is a nonzero square mod p, -1 if not, 0 if p divides a. (legendre is another function: the Legendre polynomials.)

jacobiSymbol

The Jacobi symbol (a/n) for an odd positive n. For a composite n, 1 does not mean that a is a square mod n. (jacobi is another function.)

chineseRemainder

Solves x = residues[i] (mod moduli[i]) together, the moduli need not be coprime. Gives x (the least >= 0) and modulus, or a sentence when the congruences conflict.

diophantine

All integer solutions of a x + b y = c: x = x0 + dx t, y = y0 + dy t for whole t (fields x0, y0, dx, dy; x0 the least x >= 0 when b is not 0), or a sentence when there are none.

continuedFraction

The terms [a0, a1, ...] of the continued fraction of a fraction.

continuedFractionSqrt

The continued fraction of sqrt(n): whole and the repeating period (empty for a perfect square).

convergents

The convergents of a continued fraction given by its terms, as exact fractions.

pell

The fundamental (least) solution x, y of Pell's equation x^2 - d y^2 = 1, d not a perfect square. An error if it is too large for the engine's whole numbers.

primitiveTriples

Every primitive Pythagorean triple with c up to a limit, as records a, b, c, smallest c first. Works up to a limit of 1500000.

sumOfTwoSquares

Every way of writing n as a^2 + b^2 with 0 <= a <= b, as records a, b; an empty list when there is none.

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: trial division up to a million, then the deterministic Miller-Rabin test (exact for every n below 3.3*10^24, a probable-prime test with 13 witnesses above). Works at any size (past 10^38 through big integers). Inside a sum it counts: sum(isPrime(quotient(10^k - 1, 9)), k, 2, 25) is 3, the repunit primes of up to 25 ones.

nextPrime

The smallest prime strictly greater than n, by the same test as isPrime. 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. Any n of up to 38 digits: isSquareFree(2^64 + 1) is true.

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. Works up to n = 20000000.

cousinPrimes

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

sexyPrimes

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

primePairs

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

consecutivePrimePairs

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

maxPrimeGap

The largest gap between consecutive primes up to n.

isMersennePrime

True if 2^p - 1 is prime (Lucas-Lehmer). Works up to p = 8800.

primeWithDigits

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

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.