Imaginary quadratic class number calculator
Enter a squarefree d and get the exact class number of Q(√−d): for 23 the discriminant is −23 and the class number is 3, while 1 gives the Gaussian rationals, with class number 1. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- D
- 23
- Discriminant
- -23
- Class number
- 3
This imaginary quadratic class number calculator takes a positive squarefree integer d and computes the class number of the field Q(sqrt(-d)). It first selects the field's fundamental discriminant, then deterministically counts the reduced primitive positive definite binary quadratic forms of that discriminant. The result is exact, reproducible, and accompanied by the discriminant used, making it suitable for checking number theory exercises, tables, and computational experiments without relying on a remote database.
From a squarefree radicand to a field discriminant
The input d identifies the imaginary quadratic field Q(sqrt(-d)), but the discriminant used for binary quadratic forms is not always simply -d. When d is congruent to 3 modulo 4, the fundamental discriminant is -d; in every other squarefree case it is -4d. That distinction is essential because form classes are organized by discriminant, and using the wrong one can count an order inside the field rather than the full ring of integers.
The calculator applies this rule automatically and returns the selected discriminant beside the class number, so the arithmetic convention is visible rather than hidden. The input must be a positive squarefree integer: no prime square may divide it. For example, 5 is squarefree, while 12 is not because 4 divides it.
Rejecting nonsquarefree inputs keeps the meaning unambiguous, since different radicands can otherwise describe the same quadratic field and would obscure which fundamental discriminant should govern the count. Inputs are also bounded to keep browser execution quick and predictable.
How reduced forms give the class number
For a negative fundamental discriminant D, the ideal class group of the imaginary quadratic field corresponds to equivalence classes of primitive positive definite integral binary quadratic forms ax^2+bxy+cy^2 with b^2-4ac=D. Every class has one reduced representative when the usual boundary convention is enforced. The algorithm enumerates integers a and b in finite bounds, derives c from the discriminant equation, and retains exactly the forms satisfying |b| less than or equal to a less than or equal to c.
On a boundary where |b|=a or a=c, it keeps only nonnegative b, preventing two equivalent boundary representatives from being counted twice. It also verifies gcd(a,b,c)=1, making primitivity explicit. The bound a less than or equal to sqrt(|D|/3) proves that the search terminates.
Counting the surviving representatives therefore produces the class number directly. No numerical approximation, random sampling, online table, or unproved heuristic enters the result; all tests use exact safe-integer arithmetic within the published input limit.
What you can do with it
Check a number theory exercise
Verify a hand enumeration of reduced positive definite forms for a specified imaginary quadratic field.
Test an independent implementation
Compare deterministic results against code based on ideals, analytic formulas, or another reduced-form convention.
FAQ
What inputs are accepted?
A positive squarefree integer d from 1 through 1,000,000,000.
Why must d be squarefree?
Squarefree d gives a canonical radicand for the field and lets the calculator select its fundamental discriminant without duplicate field descriptions.
Which discriminant is used?
It uses -d when d is 3 modulo 4 and -4d otherwise.
What exactly is being counted?
Primitive reduced positive definite integral binary quadratic forms of the field's fundamental discriminant, with the standard boundary convention.
Is the result approximate?
No. The finite enumeration and all divisibility tests are deterministic exact integer operations within the input limit.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/numth/class-number-imaginary \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"d":23}'const res = await fetch("https://api.kit.forhosting.com/numth/class-number-imaginary", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"d": 23
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/numth/class-number-imaginary",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"d": 23
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/numth/class-number-imaginary", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"d":23}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"d":23}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/numth/class-number-imaginary", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"d": 23
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "numth.class_number_imaginary",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_d | 1000000000 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Reading and checking the result
A class number of one means every fractional ideal is principal; larger values measure the failure of unique factorization of ideals into principal generators, not a failure of unique factorization of ideals themselves. Its strength is transparent, deterministic counting for moderate inputs. The response contains the accepted radicand d, the fundamental discriminant, and class_number, the number of reduced form classes found. Familiar checks include d=1, whose field is the Gaussian rationals and has class number one, and d=23, whose discriminant is -23 and class number is three. These small cases are useful when testing a textbook derivation or a separate implementation. For research-scale computations, remember that this endpoint deliberately uses direct reduced-form enumeration rather than faster analytic or subexponential class-group methods. The API price is $0.002 per request, while the browser version runs the same pure calculation locally.
Integration use cases
Build a small class-number table
Run squarefree radicands through a reproducible API and retain both the field discriminant and exact count.
Technical questions
What does an API call cost?
Each API request costs $0.002; the browser calculator runs locally for free.