Skip to content

Advanced

Auto-generated from src/stdlib/advanced.mcrs — do not edit manually.

API


fib v1.0.0

Fibonacci number F(n) using simple iteration. Overflow: F(46) = 1 836 311 903 ≈ INT_MAX; keep n ≤ 46.

redscript
fn fib(n: int) -> int

Parameters

ParameterDescription
nIndex (n ≥ 0)

Returns: F(n) — fib(0) == 0, fib(1) == 1, fib(10) == 55


is_prime v1.0.0

Primality test by trial division up to √n.

redscript
fn is_prime(n: int) -> int

Parameters

ParameterDescription
nInteger to test (n ≥ 0)

Returns: 1 if n is prime, 0 otherwise

Example

redscript
is_prime(2)  // 1
is_prime(4)  // 0
is_prime(97) // 1

collatz_steps v1.0.0

Number of steps in the Collatz sequence starting at n until reaching 1.

redscript
fn collatz_steps(n: int) -> int

Parameters

ParameterDescription
nStarting value (n ≥ 1)

Returns: Step count — collatz_steps(1) == 0, collatz_steps(6) == 8


digit_sum v1.0.0

Sum of decimal digits. Negative input uses the absolute value.

redscript
fn digit_sum(n: int) -> int

Parameters

ParameterDescription
nInteger input

Returns: Sum of digits — digit_sum(123) == 6, digit_sum(0) == 0


count_digits v1.0.0

Count decimal digits of n. 0 has 1 digit; negative values count absolute digits.

redscript
fn count_digits(n: int) -> int

Parameters

ParameterDescription
nInteger input

Returns: Digit count — count_digits(0) == 1, count_digits(100) == 3


reverse_int v1.0.0

Reverse the decimal digits of an integer. Sign is preserved.

redscript
fn reverse_int(n: int) -> int

Parameters

ParameterDescription
nInteger input

Returns: Digit-reversed integer — reverse_int(12345) == 54321, reverse_int(-42) == -24


mod_pow v1.0.0

Modular exponentiation: (base ^ exp) mod m using O(log exp) binary squaring. ⚠ m must be ≤ 46 340 to avoid b*b int32 overflow.

redscript
fn mod_pow(base: int, exp: int, m: int) -> int

Parameters

ParameterDescription
baseBase value
expExponent (≥ 0)
mModulus (1 < m ≤ 46340)

Returns: (base^exp) mod m — mod_pow(2, 10, 1000) == 24


hash_int v1.0.0

Deterministic integer hash. Output is non-negative [0, ~2×10⁹). Same input always produces the same output — useful as a seeded pseudo-random value.

redscript
fn hash_int(n: int) -> int

Parameters

ParameterDescription
nInput integer (any value)

Returns: Non-negative hash value


noise1d v1.0.0

1-D value noise with C¹ continuity via smoothstep interpolation. Input x is fixed-point (scale = 1000). Output in [0, 999].

redscript
fn noise1d(x: int) -> int

Parameters

ParameterDescription
xCoordinate × 1000 (e.g. 500 = 0.5, 1000 = 1.0)

Returns: Smoothly interpolated noise value in [0, 999]


bezier_quad v1.0.0

Quadratic Bezier curve evaluated at t using De Casteljau's algorithm.

redscript
fn bezier_quad(p0: int, p1: int, p2: int, t: int) -> int

Parameters

ParameterDescription
p0Start control point
p1Middle control point
p2End control point
tParameter × 1000 (0 = start, 1000 = end)

Returns: Curve value at t

Example

redscript
bezier_quad(0, 500, 1000, 500)  // 500  (midpoint)
bezier_quad(0, 1000, 0, 500)    // 500  (arch at midpoint)

bezier_cubic v1.0.0

Cubic Bezier curve evaluated at t using De Casteljau's algorithm.

redscript
fn bezier_cubic(p0: int, p1: int, p2: int, p3: int, t: int) -> int

Parameters

ParameterDescription
p0Control point 0 (start)
p1Control point 1
p2Control point 2
p3Control point 3 (end)
tParameter × 1000 (0 = start, 1000 = end)

Returns: Curve value at t


mandelbrot_iter v1.0.0

Mandelbrot set iteration count for complex number c = cx/1000 + i·cy/1000. Returns the escape iteration count, or max_iter if the point is in the set. Use the return value to colour blocks!

redscript
fn mandelbrot_iter(cx: int, cy: int, max_iter: int) -> int

Parameters

ParameterDescription
cxReal part × 1000 (range −2000..1000)
cyImaginary part × 1000 (range −1000..1000)
max_iterMaximum iteration count

Returns: Iterations before |z| > 2, or max_iter if in the set

Example

redscript
mandelbrot_iter(-1000, 0, 100) // 100 — c = -1+0i is in the set
mandelbrot_iter(1000, 0, 100)  //   0 — c = 1+0i escapes immediately

julia_iter v1.0.0

Julia set iteration count with fixed constant c and variable starting point z₀. Same escape condition as mandelbrot_iter.

redscript
fn julia_iter(z0r: int, z0i: int, cr: int, ci: int, max_iter: int) -> int

Parameters

ParameterDescription
z0rReal part of starting point × 1000
z0iImaginary part of starting point × 1000
crReal part of constant c × 1000
ciImaginary part of constant c × 1000
max_iterMaximum iteration count

Returns: Iterations before |z| > 2, or max_iter if in the set


angle_between v1.0.0

Unsigned angle (0–180°) between two 2-D integer vectors.

redscript
fn angle_between(x1: int, y1: int, x2: int, y2: int) -> int

Parameters

ParameterDescription
x1X component of vector 1
y1Y component of vector 1
x2X component of vector 2
y2Y component of vector 2

Returns: Angle in whole degrees [0, 180]; 0 for zero-length inputs

Example

redscript
angle_between(1000, 0, 0, 1000)  // 90
angle_between(1000, 0, -1000, 0) // 180

clamp_circle_x v1.0.0

X component of the point (x, y) clamped to a circle of radius r centred at the origin. Keep |x|, |y| < ~2000 to avoid overflow in normalize2d_x.

redscript
fn clamp_circle_x(x: int, y: int, r: int) -> int

Parameters

ParameterDescription
xX coordinate (raw block coords, not fixed-point)
yY coordinate
rCircle radius in the same units as x/y

Returns: Clamped X — clamp_circle_x(600, 0, 500) == 500


clamp_circle_y v1.0.0

Y component of the point (x, y) clamped to a circle of radius r centred at the origin.

redscript
fn clamp_circle_y(x: int, y: int, r: int) -> int

Parameters

ParameterDescription
xX coordinate
yY coordinate
rCircle radius

Returns: Clamped Y — clamp_circle_y(0, 600, 500) == 500


newton_sqrt v1.0.0

Integer square root via Newton's method (alternative to isqrt). Converges quadratically; useful for validating while-loop + division logic.

redscript
fn newton_sqrt(n: int) -> int

Parameters

ParameterDescription
nNon-negative integer

Returns: ⌊√n⌋ — newton_sqrt(25) == 5, newton_sqrt(100) == 10


bezier_quartic v1.0.0

Quartic (5-point) Bezier curve evaluated at t using De Casteljau's algorithm.

redscript
fn bezier_quartic(p0: int, p1: int, p2: int, p3: int, p4: int, t: int) -> int

Parameters

ParameterDescription
p0Control point 0 (start)
p1Control point 1
p2Control point 2
p3Control point 3
p4Control point 4 (end)
tParameter × 1000 (0 = start, 1000 = end)

Returns: Curve value at t


bezier_n v1.0.0

Arbitrary-degree Bezier curve via De Casteljau's algorithm. ⚠ Modifies pts in-place. Use bezier_n_safe to preserve the original array.

redscript
fn bezier_n(pts: int[], n: int, t: int) -> int

Parameters

ParameterDescription
ptsArray of n control points (modified in-place during evaluation)
nNumber of control points
tParameter × 1000 (0 = start, 1000 = end)

Returns: Curve value at t


bezier_n_safe v1.0.0

Non-destructive arbitrary-degree Bezier curve. Copies pts into work then evaluates.

redscript
fn bezier_n_safe(pts: int[], work: int[], n: int, t: int) -> int

Parameters

ParameterDescription
ptsArray of n control points (not modified)
workWorking buffer of length ≥ n (will be overwritten)
nNumber of control points
tParameter × 1000 (0 = start, 1000 = end)

Returns: Curve value at t


digital_root v1.0.0

Digital root: repeatedly sum digits until a single digit remains.

redscript
fn digital_root(n: int) -> int

Parameters

ParameterDescription
nNon-negative integer

Returns: Single-digit root — digital_root(493) == 7, digital_root(0) == 0


spiral_ring v1.0.0

Ulam spiral ring number: which concentric square ring contains n? Ring 0: n=1; Ring 1: n=2..9 (3×3); Ring 2: n=10..25 (5×5); etc.

redscript
fn spiral_ring(n: int) -> int

Parameters

ParameterDescription
nPositive integer

Returns: Ring index — spiral_ring(1) == 0, spiral_ring(9) == 1, spiral_ring(25) == 2


median v1.1.0

Median of n integers. Sorts a copy of the array; the original is not modified. For odd n returns the middle value × 1000. For even n returns the average × 1000.

redscript
fn median(arr: int[], work: int[], n: int) -> int

Parameters

ParameterDescription
arrInput array (not modified)
workWorking buffer of length ≥ n (will be overwritten)
nNumber of elements (must be ≥ 1)

Returns: Median × 1000


mode v1.1.0

Most frequent element (mode) in arr[0..n). Tie-breaks toward the smallest value.

redscript
fn mode(arr: int[], work: int[], n: int) -> int

Parameters

ParameterDescription
arrInput array (not modified)
workWorking buffer of length ≥ n (will be overwritten)
nNumber of elements (must be ≥ 1)

Returns: The modal value


mean_fx v1.1.0

Arithmetic mean as fixed-point × 1000.

redscript
fn mean_fx(arr: int[], n: int) -> int

Parameters

ParameterDescription
arrInput array of integers
nNumber of elements (must be ≥ 1)

Returns: sum(arr) × 1000 / n; 0 for empty array


std_dev_fx v1.1.0

Population standard deviation as fixed-point × 1000. Uses integer arithmetic; values must fit in int32 before squaring deviations.

redscript
fn std_dev_fx(arr: int[], n: int) -> int

Parameters

ParameterDescription
arrInput array of integers
nNumber of elements (must be ≥ 2)

Returns: √(Σ(xᵢ−mean)² / n) × 1000; 0 for n ≤ 1


hermite_spline v1.1.0

Cubic Hermite spline interpolation between two endpoints with explicit tangents.

redscript
fn hermite_spline(p0: int, p1: int, m0: int, m1: int, t: int) -> int

Parameters

ParameterDescription
p0Start value
p1End value
m0Tangent at p0
m1Tangent at p1
tParameter × 1000 (0 = p0, 1000 = p1)

Returns: Interpolated value (same scale as p0/p1)


catmull_rom v1.1.0

Catmull-Rom spline: interpolates between p1 and p2 with tangents derived from neighbours. Tangents: m1 = (p2 − p0) / 2, m2 = (p3 − p1) / 2.

redscript
fn catmull_rom(p0: int, p1: int, p2: int, p3: int, t: int) -> int

Parameters

ParameterDescription
p0Point before the segment start
p1Segment start
p2Segment end
p3Point after the segment end
tParameter × 1000 (0 = p1, 1000 = p2)

Returns: Interpolated value between p1 and p2


Released under the MIT License.