πŸ”’ hpmath

High-Precision Math Engine in Pure C

Pure C Zero Dependencies 22 Endpoints Arbitrary Precision HTTP API Educational Focus

Status: checking...

πŸ“– Introduction

hpmath is a production-ready, arbitrary-precision mathematics engine written in pure C. It serves via HTTP with 22 endpoints covering number theory, sequences, modular arithmetic, and perfect powers.

✨ Why hpmath?

🎯 Use Cases

Note: All numeric parameters and results are JSON strings (e.g., "12345" not 12345) to preserve arbitrary precision across parsers.

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” HTTP/JSON β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Your App │◄──────────────────►│ hpmath (C) β”‚ β”‚ (OCaml/Rust/ β”‚ POST /api/... β”‚ β€’ BigInt β”‚ β”‚ Python/Elm) β”‚ {"n":"123"} β”‚ β€’ Number theoryβ”‚ β”‚ │◄──────────────────►│ β€’ Sequences β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ {"result":"..."} β”‚ β€’ Modular math β”‚ (quoted strings) β”‚ β€’ Constants β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Principles

Project Structure

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

πŸ“‘ Endpoints Catalog

Base URL: http://127.0.0.1:8002/api

Core Arithmetic

POST /api/integer/gcd

Greatest Common Divisor of two integers using Euclidean algorithm.

POST /api/integer/lcm

Least Common Multiple: |aΓ—b| / gcd(a,b)

POST /api/integer/compare

Compare two arbitrary-precision integers.

Primes & Factorization

POST /api/integer/is-prime

Primality test (trial division for educational ranges).

POST /api/integer/factorize

Prime factorization as array of strings.

πŸ”— Integration Guide

OCaml (gtmath)

(* 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

Python

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

curl (Testing)

# 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\"}"
Golden Rule: Always send and receive numbers as JSON strings. Parsers like JavaScript's JSON.parse() will overflow BigInts as floats otherwise.

πŸ’‘ Example Problems

NCERT Class 10: HCF by Factorization

"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"} βœ“

Power Comparison (Competitive Programming)

"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

Modular Exponentiation (Cryptography)

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

πŸ§ͺ Interactive Test UI

All endpoints below call your local hpmath server. Results display as formatted JSON.

GET /health