High-Precision Math Engine in Pure C
Status: checking...
"12345" not 12345)
to preserve arbitrary precision across parsers.
hpmath/
βββ src/
β βββ bignum/ # BigInt, BigRational implementation
β βββ radical/ # Symbolic radical simplification
β βββ decimalexpand/ # Decimal expansion, constants Ο/e/Ο
β βββ numbertheory/ # GCD, primes, factorization, sequences, modpow
βββ cserver/
β βββ main.c # HTTP server loop (~500 lines pure C)
β βββ router.c # Endpoint routing
β βββ handlers/ # Thin HTTP handlers (parse JSON β call lib β respond)
β βββ json_helpers.c # Minimal JSON parser (no external lib)
βββ include/ # Public headers (bigint.h, bigrational.h, etc.)
βββ static/ # This documentation + test UI
βββ Makefile # Zero-dependency build
βββ bin/hpmath-server # Compiled binary
Base URL: http://127.0.0.1:8002/api
/api/integer/gcd
Greatest Common Divisor of two integers using Euclidean algorithm.
/api/integer/lcm
Least Common Multiple: |aΓb| / gcd(a,b)
/api/integer/compare
Compare two arbitrary-precision integers.
/api/integer/is-prime
Primality test (trial division for educational ranges).
/api/integer/factorize
Prime factorization as array of strings.
(* lib/numeric/hpmath_client.ml *)
module Hpmath = struct
type config = { base_url: string; timeout: float }
let default = { base_url = "http://127.0.0.1:8002"; timeout = 30.0 }
let post ~config ~endpoint ~params =
let json = Yojson.Basic.to_string (`Assoc params) in
Http.post (config.base_url ^ endpoint) ~headers:[("Content-Type","application/json")] ~body:json
let factorial ~config n =
post ~config ~endpoint:"/api/integer/factorial"
~params:["n", `String (Bigint.to_string n)]
|> Json.parse |> fun j -> Bigint.of_string (Json.get "factorial" j)
end
import requests
def hpmath_factorial(n: int) -> int:
resp = requests.post(
"http://127.0.0.1:8002/api/integer/factorial",
json={"n": str(n)}, # β String, not int!
headers={"Content-Type": "application/json"}
)
return int(resp.json()["factorial"]) # Parse quoted string
# GCD
curl -s -X POST http://127.0.0.1:8002/api/integer/gcd \
-H "Content-Type: application/json" \
-d '{"a":"96","b":"404"}'
# Power comparison: 100^98 vs 89^100
A=$(curl -s -X POST http://127.0.0.1:8002/api/integer/power \
-d '{"base":"100","exp":"98"}' | jq -r '.power')
B=$(curl -s -X POST http://127.0.0.1:8002/api/integer/power \
-d '{"base":"89","exp":"100"}' | jq -r '.power')
curl -s -X POST http://127.0.0.1:8002/api/integer/compare \
-d "{\"a\":\"$A\",\"b\":\"$B\"}"
JSON.parse() will overflow BigInts as floats otherwise.
"Find the HCF of 96 and 404 by prime factorization."
# Step 1: Factorize both
curl -s -X POST http://127.0.0.1:8002/api/integer/factorize -d '{"n":"96"}'
# β {"factors":["2","2","2","2","2","3"]}
curl -s -X POST http://127.0.0.1:8002/api/integer/factorize -d '{"n":"404"}'
# β {"factors":["2","2","101"]}
# Step 2: Compute GCD (or intersect factors manually)
curl -s -X POST http://127.0.0.1:8002/api/integer/gcd -d '{"a":"96","b":"404"}'
# β {"gcd":"4"} β
"Which is larger: 2^1000 or 3^600?"
# Compute both (results are quoted strings)
A=$(curl -s -X POST .../power -d '{"base":"2","exp":"1000"}' | jq -r '.power')
B=$(curl -s -X POST .../power -d '{"base":"3","exp":"600"}' | jq -r '.power')
# Compare
curl -s -X POST .../compare -d "{\"a\":\"$A\",\"b\":\"$B\"}"
# β {"comparison":1,"relation":"greater"} β 2^1000 > 3^600
# Compute (2^1000) mod 1000000007 efficiently
curl -s -X POST http://127.0.0.1:8002/api/integer/modpow \
-d '{"base":"2","exp":"1000","mod":"1000000007"}'
# β {"modpow":"14641"} β (computed in O(log exp) time)
All endpoints below call your local hpmath server. Results display as formatted JSON.
/health