{
  "openapi": "3.1.1",
  "info": {
    "title": "Number-Theory Lab API",
    "version": "1.0.0",
    "summary": "Primality, factorisation, gcd, modular arithmetic, CRT, φ, π(n), Collatz, continued fractions.",
    "description": "Every endpoint performs the same calculation as the human page and returns inputs, result, units, assumptions, sources, warnings and computedAt. MCP endpoint: /mcp."
  },
  "servers": [
    {
      "url": "https://numbertheorylab.usefulweb.site"
    }
  ],
  "paths": {
    "/api/v1/calculate": {
      "post": {
        "operationId": "calculate",
        "summary": "Primality, factorisation, gcd, modular arithmetic, CRT, φ, π(n), Collatz, continued fractions.",
        "description": "Big-integer number theory with the working shown: Miller–Rabin primality (deterministic up to 3.3·10^24, otherwise with a stated number of rounds), factorisation by trial division and Pollard rho, gcd/lcm with the extended Euclidean table, modular inverse and exponentiation, the Chinese remainder theorem, Euler φ and divisors, prime counting up to 10 million, Collatz sequences and continued fractions.\n\nMethodology:\n- Primality: trial division by primes below 100, then Miller–Rabin. Below 3,317,044,064,679,887,385,961,981 the first 13 primes as bases form a proof (Sorenson & Webster 2015), which covers every 64-bit number. Above it, the stated number of rounds uses bases derived from SHA-256 of n (reproducible); a composite passes each round with probability at most 1/4.\n- Factorisation: trial division up to 10,000, then Pollard–Brent rho with a fixed iteration budget; factors are checked with Miller–Rabin. If the budget runs out the remaining cofactor is reported as unfactored, never guessed.\n- φ(n) = ∏ p^(e−1)(p − 1), σ(n) = ∏ (p^(e+1) − 1)/(p − 1), d(n) = ∏ (e + 1). Carmichael numbers are detected with Korselt's criterion (square-free, composite, p − 1 | n − 1 for every prime factor).\n- gcd via the Euclidean algorithm with Bézout coefficients; inverse exists iff gcd(a, m) = 1. CRT merges congruences pairwise and also handles non-coprime moduli (or reports that none exists). π(n) uses an odd-only sieve of Eratosthenes.",
        "tags": [
          "math"
        ],
        "x-mcp-tool": "number_theory_lab",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NumberTheoryLabInput"
              },
              "examples": {
                "example1": {
                  "summary": "561, the smallest Carmichael number",
                  "value": {
                    "operation": "analyze",
                    "a": "561"
                  }
                },
                "example2": {
                  "summary": "Factor 2^64 + 1",
                  "value": {
                    "operation": "analyze",
                    "a": "2^64+1"
                  }
                },
                "example3": {
                  "summary": "Extended Euclid of 240 and 46",
                  "value": {
                    "operation": "gcd",
                    "a": "240",
                    "b": "46"
                  }
                },
                "example4": {
                  "summary": "CRT: x ≡ 2 (3), 3 (5), 2 (7)",
                  "value": {
                    "operation": "crt",
                    "congruences": "2 3\n3 5\n2 7"
                  }
                },
                "example5": {
                  "summary": "π(10^6)",
                  "value": {
                    "operation": "primecount",
                    "a": "1000000"
                  }
                },
                "example6": {
                  "summary": "Continued fraction of √7",
                  "value": {
                    "operation": "cf",
                    "a": "7"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Calculation result envelope.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/NumberTheoryLabResult"
                }
              }
            }
          },
          "400": {
            "description": "Malformed JSON body.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "Invalid input (see errors[]).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded (see Retry-After).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/tool": {
      "get": {
        "operationId": "getToolDefinition",
        "summary": "Inputs, output schema and examples for this tool",
        "responses": {
          "200": {
            "description": "Tool definition.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Problem": {
        "type": "object",
        "description": "RFC 9457 problem details. 422 responses include per-field `errors`.",
        "required": [
          "type",
          "title",
          "status",
          "detail"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "detail": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "field",
                "message"
              ],
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "NumberTheoryLabInput": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "operation": {
            "title": "Operation",
            "type": "string",
            "enum": [
              "analyze",
              "gcd",
              "modpow",
              "modinv",
              "crt",
              "primecount",
              "collatz",
              "cf"
            ],
            "default": "analyze"
          },
          "a": {
            "title": "n or a",
            "description": "Integer: decimal, 0x hex, 0b binary, or a power such as 2^127-1.",
            "type": "string",
            "maxLength": 1300,
            "default": "561"
          },
          "b": {
            "title": "b (second number / exponent / denominator)",
            "type": "string",
            "maxLength": 1300
          },
          "m": {
            "title": "Modulus m",
            "type": "string",
            "maxLength": 1300
          },
          "congruences": {
            "title": "Congruences (one per line: remainder modulus)",
            "description": "For the CRT: each line \"a m\" means x ≡ a (mod m). Moduli need not be coprime.",
            "type": "string",
            "maxLength": 4000
          },
          "rounds": {
            "title": "Miller–Rabin rounds above 3.3·10^24",
            "description": "Error probability at most 4^−rounds for numbers beyond the deterministic range.",
            "type": "integer",
            "minimum": 1,
            "maximum": 64,
            "default": 40
          }
        },
        "required": []
      },
      "NumberTheoryLabResult": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "tool",
          "computedAt",
          "inputs",
          "summary",
          "result",
          "units",
          "assumptions",
          "sources",
          "warnings",
          "presentation",
          "links"
        ],
        "properties": {
          "schemaVersion": {
            "type": "string",
            "const": "1.0"
          },
          "tool": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "id",
              "name",
              "version"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "version": {
                "type": "string"
              }
            }
          },
          "computedAt": {
            "type": "string",
            "format": "date-time"
          },
          "inputs": {
            "type": "object",
            "description": "Normalised inputs actually used, including applied defaults."
          },
          "summary": {
            "type": "string"
          },
          "result": {
            "type": "object",
            "required": [
              "operation",
              "answer"
            ],
            "properties": {
              "operation": {
                "type": "string"
              },
              "answer": {
                "type": "string",
                "description": "Main result as text (big integers as decimal strings)."
              },
              "n": {
                "type": "string"
              },
              "isPrime": {
                "type": "boolean"
              },
              "primalityMethod": {
                "type": "string"
              },
              "errorBound": {
                "type": "number"
              },
              "basesTested": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "factors": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "p",
                    "e"
                  ],
                  "properties": {
                    "p": {
                      "type": "string"
                    },
                    "e": {
                      "type": "integer"
                    }
                  }
                }
              },
              "factorisationComplete": {
                "type": "boolean"
              },
              "phi": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "divisorCount": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "divisorSum": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "divisors": {
                "type": [
                  "array",
                  "null"
                ],
                "items": {
                  "type": "string"
                }
              },
              "carmichael": {
                "type": "boolean"
              },
              "gcd": {
                "type": "string"
              },
              "lcm": {
                "type": "string"
              },
              "bezout": {
                "type": "object",
                "properties": {
                  "x": {
                    "type": "string"
                  },
                  "y": {
                    "type": "string"
                  }
                }
              },
              "value": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "modulus": {
                "type": "string"
              },
              "count": {
                "type": "integer"
              },
              "steps": {
                "type": "integer"
              },
              "peak": {
                "type": "string"
              },
              "terms": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "period": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "convergents": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          },
          "units": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "assumptions": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "sources": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "title"
              ],
              "properties": {
                "title": {
                  "type": "string"
                },
                "url": {
                  "type": "string"
                },
                "publisher": {
                  "type": "string"
                },
                "retrievedAt": {
                  "type": "string"
                },
                "note": {
                  "type": "string"
                }
              }
            }
          },
          "warnings": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "presentation": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Display blocks used by the human page (same data)."
          },
          "links": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "human",
              "api",
              "openapi",
              "methodology"
            ],
            "properties": {
              "human": {
                "type": "string",
                "format": "uri"
              },
              "api": {
                "type": "string",
                "format": "uri"
              },
              "openapi": {
                "type": "string",
                "format": "uri"
              },
              "methodology": {
                "type": "string",
                "format": "uri"
              }
            }
          }
        }
      }
    }
  }
}