ForHosting KIT · Math

Greatest common divisor

Enter two integers and get their greatest common divisor by Euclid's method: 48 and 18 give 6 after three remainder steps, and 1071 and 462 give 21. Minus signs are ignored, so −48 and 18 also give 6. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
A
48
B
18
You get
Gcd
6

The greatest common divisor of two integers is the largest positive integer that divides both without a remainder. Students meet it when reducing fractions, programmers meet it in contest warm-ups, and engineers meet it whenever two periods, gear teeth, or tile sizes must share a common measure. This calculator accepts two integers, runs the classical Euclidean algorithm on their absolute values, and returns the non-negative GCD together with the number of remainder steps it took.

What the greatest common divisor is and how Euclid finds it

The greatest common divisor of two integers a and b is the largest positive integer d that divides both a and b with remainder zero. Classic classroom checks include forty-eight and eighteen, whose common divisors are one, two, three, and six, so the greatest is six; and one thousand seventy-one and four hundred sixty-two, whose greatest common divisor is twenty-one. Euclid’s algorithm never lists every divisor.

It uses the identity that any common divisor of a and b is also a common divisor of b and the remainder a modulo b, so you may replace the pair with the smaller number and that remainder and repeat until the remainder is zero. The last non-zero remainder is the GCD. Because JavaScript remainder keeps the sign of the dividend, this calculator first takes absolute values, then runs the loop, and always reports a non-negative GCD.

That matches the usual mathematical convention: gcd of negative twelve and eighteen is still six. When one argument is zero and the other is not, the GCD is the absolute value of the non-zero argument, because every integer divides zero.

What you enter and what comes back

Values must be finite safe integers, so magnitudes beyond the JavaScript safe integer ceiling are rejected instead of silently losing low bits. Missing a, missing b, empty strings, and non-numeric types also fail, and those failures are not billed. On success the body includes the echoed integers a and b, the non-negative gcd, the remainder-step count, and the method string euclidean.

Example: a equals forty-eight and b equals eighteen yields gcd six after three remainder steps, because forty-eight modulo eighteen is twelve, eighteen modulo twelve is six, and twelve modulo six is zero. Example: a equals negative forty-eight and b equals eighteen still yields gcd six, because signs are stripped before the loop.

Where a two-integer GCD calculator earns its keep

Classrooms and tutoring platforms use a greatest common divisor calculator so learners can check a handwritten Euclidean remainder table against a canonical result without waiting for a teacher to catch a dropped remainder. Exam generators and learning-management plugins need a deterministic oracle that rejects twelve-point-five instead of silently truncating it to twelve and marking a lucky guess correct.

Sibling pages cover a combined GCD and LCM list calculator, a coprime checker that highlights whether the gcd equals one, the extended Euclidean algorithm that also returns Bézout coefficients, and polynomial GCD over coefficient arrays; this landing answers only the two-integer greatest common divisor question. Failed validation does not consume balance. Keep requests inside the safe-integer bounds, pin the gcd in tests, and you get reproducible divisors for homework, LMS plugins, and edge workers without standing up symbolic math infrastructure of your own.

Classroom Euclidean remainder checks

Let students submit two integers and verify the GCD and remainder-step count against the Euclidean table they wrote by hand.

Fraction reduction to lowest terms

Divide a numerator and a denominator by their GCD so a ratio is already in lowest terms before display, grading, or further arithmetic.

Shared measures in schedules and gears

Find the largest integer that measures two periods, tooth counts, or tile lengths so repeating patterns line up without a full computer-algebra library.

How do I compute the greatest common divisor of two integers?

The response includes the non-negative gcd, the Euclidean remainder-step count, and method set to euclidean.

Does the Euclidean algorithm work with negatives and zero?

Yes. Signs are stripped first, so gcd(-48, 18) is 6. gcd(n, 0) is |n| when the other value is non-zero. Both inputs zero is rejected as undefined.

How is this different from a GCD and LCM calculator or a coprime checker?

This page returns only the greatest common divisor of two integers. Combined GCD-LCM, coprime flags, Bézout coefficients, and polynomial GCD are separate landings.

What does the number of steps tell me?

It counts the remainder divisions Euclid's method needed. For 48 and 18 there are three: 48 divided by 18 leaves 12, 18 divided by 12 leaves 6, and 12 divided by 6 leaves nothing.

Can I enter a decimal such as 12.5?

No. Both values must be whole numbers, so a decimal is refused instead of being quietly cut down to an integer, which would give a plausible but wrong divisor.

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.

POSThttps://api.kit.forhosting.com/math/greatest-common-divisor

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.

curl -X POST https://api.kit.forhosting.com/math/greatest-common-divisor \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"a":48,"b":18}'
{
  "a": 48,
  "b": 18
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "math.greatest_common_divisor",
  "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.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_abs9007199254740991
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Non-integers, missing fields, and the undefined pair zero and zero fail with invalid_input and are not billed.

What the greatest common divisor is and how Euclid finds it

When both arguments are zero there is no greatest positive common divisor, so the request is rejected rather than returning zero. The response echoes the parsed integers a and b, returns gcd as that non-negative result, reports steps as the number of remainder operations performed in the given order, and sets method to euclidean so clients can label the algorithm without sniffing the payload.

Input contract, response shape, and error behaviour

POST a JSON body with two required integer fields named a and b. Each field may arrive as a JSON integer or as a trimmed decimal string with an optional leading sign; embedded spaces, fractions, scientific notation, booleans, objects, and leading zeros on multi-digit strings fail with invalid_input naming the field. Pair the numeric gcd with your own fraction-reduction code if you need a reduced numerator and denominator; this endpoint computes only the divisor itself and does not emit prime factors, Bézout coefficients, or a least common multiple. There is no randomness, no clock, and no network: two identical requests always produce the same JSON.

Where a two-integer GCD calculator earns its keep

Scheduling tools, gear-ratio notebooks, and tiling scripts often need the largest shared measure of two integer lengths so a repeating pattern lines up; calling a tiny parse API avoids pulling a computer-algebra system for a two-number remainder loop.

What happens if I send a decimal instead of an integer?

The call fails with invalid_input naming the field. This calculator does not truncate 12.5 to 12; failed validation is not billed.

What does the greatest common divisor calculator cost?

The browser widget is free and runs locally. API calls use prepaid KIT balance at the published $0.002 base price; invalid input is not charged.

Read the full KIT documentation →