Functional Weave
Code in Rust

math.integer-sqrt

Exact integer square root: the largest whole number whose square does not exceed n.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 11 tests, run in TypeScript, Python and Rust.

What it does

The integer square root of `n`: the largest whole number `r` with `r * r <= n`, so `integerSqrt(24)` is 4 and `integerSqrt(25)` is 5.

It exists so that capabilities which need a square root of a whole number (economic order quantity, grid layouts) get the same answer in every language without going through floating point. `Math.floor(Math.sqrt(n))` is right for small numbers, but the double nearest to `n` is not `n` above 2^53 and the square root of a number just below a perfect square can round up to it, so each language here corrects the floating-point guess with exact integer arithmetic (TypeScript `bigint`, Python `math.isqrt`, Rust `i128`).

For example

  • integer_sqrt(0) → 0 zero
  • integer_sqrt(1) → 1 one
  • integer_sqrt(2) → 1 two truncates to one

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

pub fn integer_sqrt(n: i64) -> i64
ninta whole number from 0 to 2^53 - 1
returnsintfloor(sqrt(n)), exactly

Your code names it in one line, in the file that uses it

fune!(math.integer-sqrt@^1);  // then call integer_sqrt(…)
impl/rust.rs · 33 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

const MAX_SAFE: i64 = (1i64 << 53) - 1;

/// The largest whole number whose square does not exceed `n`.
///
/// The floating-point square root is only a first guess: it is corrected with
/// exact `i128` arithmetic, so a number just below a perfect square never
/// rounds up to its root.
///
/// # Panics
/// Panics if `n` is negative or above 2^53 - 1.
pub fn integer_sqrt(n: i64) -> i64 {
    if !(0..=MAX_SAFE).contains(&n) {
        panic!("n must be a whole number from 0 to 2^53 - 1, received {}", n);
    }
    let target = n as i128;
    let mut r = (n as f64).sqrt().floor() as i128;
    while r * r > target {
        r -= 1;
    }
    while (r + 1) * (r + 1) <= target {
        r += 1;
    }
    r as i64
}

pub fn fune_vector(args: &[Value]) -> Value {
    if let Value::Float(f) = &args[0] {
        panic!("n must be a whole number from 0 to 2^53 - 1, received {}", f);
    }
    Value::Int(integer_sqrt(args[0].as_i64()))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Rust package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add math.integer-sqrt
Download for Rust math.integer-sqrt-1.0.0-rust.fune · 4,418 bytes sha256 c0f88b99d78f700750d642240134801c6c569b0806a915c3339f7d2366d437fe

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./math.integer-sqrt-1.0.0-rust.fune, or fetch it from a terminal with fune pull math.integer-sqrt@1.0.0:rust.

The whole function, every language, is one file too: math.integer-sqrt-1.0.0.fune, 5,586 bytes, sha256 7dc41dbf6a7e06cbd778a22b43f531a03b95d9e48c91f9ab7de1cbf549641f8c. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before math.integer-sqrt

after — your function gets the result and the arguments, and returns the final result.

// fune: after math.integer-sqrt

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show math.integer-sqrt --steps.

// fune: step math.integer-sqrt after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
zero 0 → 0
one 1 → 1
two truncates to one 2 → 1
just below a perfect square 24 → 4
a perfect square 25 → 5
just above a perfect square 26 → 5
a large perfect square, 94906265^2 9,007,199,136,250,225 → 94,906,265
one below a large perfect square: the double sqrt rounds up to 94906265, the answer is 94906264 9,007,199,136,250,224 → 94,906,264
the largest allowed input, 2^53 - 1 9,007,199,254,740,991 → 94,906,265
negative is an error -1 → error: n must be a whole number from 0 to 2^53 - 1
Show the other 1 test
CaseArgumentsExpected
a fraction is an error 2.5 → error: n must be a whole number from 0 to 2^53 - 1

More from the author

Inputs are limited to 0 .. 2^53 - 1, the range where a JavaScript number is still an exact integer, so the three languages agree on every answer. A negative or fractional input, or one beyond that range, is an error.

To round a square root of a fraction `p / q` rather than truncate it, note that `floor(sqrt(p / q)) = integerSqrt(floor(p / q))`: the floor of a square root only changes at whole numbers. `inventory.eoq` uses this.

Files

PathBytes
README.md1,110
impl/python.py483
impl/rust.rs970
impl/typescript.ts612
vectors.json960