This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Small algorithms

Classic numerical algorithms, each a few lines long. Run a cell to see its output beside the expected one.

Newton's square root

Repeat x = (x + a/x) / 2; each step roughly doubles the number of correct digits.

Viv [1]

Bisection

Root of cos(x) on [1, 2]. Each step halves the interval, so 40 halvings leave it 2^-40 ≈ 9e-13 wide and give pi/2 to about twelve digits; show prints the first six.

Viv [2]

gcd via mod

Euclid's algorithm. The floats hold whole numbers exactly, so mod is exact.

Viv [3]

Horner evaluation

A polynomial as an array of coefficients, highest power first.

Viv [4]

The power method

The dominant eigenvalue of a symmetric 2x2, found by repeated matrix-vector products; compare eig_sym.

Viv [5]

Compensated summation

Kahan's method carries each addition's rounding error into the next one. Adding 0.1 a million times this way gives exactly 100000.

Viv [6]

Fibonacci two ways

Naive recursion, whose number of calls grows exponentially with n, against a loop that takes n steps.

Viv [7]