GCD and LCM Calculator API: The Complete Guide

Need to calculate GCD or LCM in your application? This guide covers everything you need to know about GCD and LCM calculations via API, including algorithms, the relationship between them, and implementation examples.

What are GCD and LCM?

Greatest Common Divisor (GCD)

The GCD of two or more integers is the largest positive integer that divides each of the integers. Also called Greatest Common Factor (GCF) or Highest Common Factor (HCF).

GCD(12, 18) = 6
GCD(48, 60) = 12

Least Common Multiple (LCM)

The LCM of two or more integers is the smallest positive integer that is divisible by each of them.

LCM(4, 6) = 12
LCM(3, 5) = 15

GCD-LCM Relationship

GCD and LCM are related by this formula:

GCD(a, b) × LCM(a, b) = a × b
LCM(a, b) = (a × b) / GCD(a, b)
Algorithm: The API uses the efficient Euclidean algorithm for GCD calculation: GCD(a, b) = GCD(b, a mod b) until b = 0.

Using the GCD/LCM API

TinyFn provides endpoints for both GCD and LCM calculations:

API Request
GET https://api.tinyfn.io/v1/math/gcd-lcm
Headers: X-API-Key: your-api-key
Response
{
  "numbers": [12, 18, 24],
  "gcd": 6,
  "lcm": 72
}

Parameters

Parameter Type Description
numbers array Array of positive integers (at least 2)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/math/gcd-lcm?numbers=12,18,24',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(data.gcd); // 6
console.log(data.lcm); // 72

Python

import requests

response = requests.get(
    'https://api.tinyfn.io/v1/math/gcd-lcm',
    params={'numbers': '12,18,24'},
    headers={'X-API-Key': 'your-api-key'}
)
data = response.json()
print(data['gcd'])  # 6
print(data['lcm'])  # 72

cURL

curl "https://api.tinyfn.io/v1/math/gcd-lcm?numbers=12,18,24" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Fraction Simplification: Use GCD to reduce fractions
  • Scheduling: LCM for finding common time intervals
  • Cryptography: GCD in RSA key generation
  • Music Theory: Finding common rhythmic patterns
  • Engineering: Gear ratio calculations

Best Practices

  1. Use positive integers: GCD/LCM are defined for positive integers
  2. Handle zero: GCD(a, 0) = a; LCM with 0 is typically 0
  3. Multiple numbers: Apply iteratively: GCD(a,b,c) = GCD(GCD(a,b),c)
  4. Watch overflow: LCM can be very large; consider using BigInt

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-math": {
      "url": "https://api.tinyfn.io/mcp/math/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all math tools available via MCP in our Math MCP Tools for AI Agents guide.

Try the GCD/LCM Calculator API

Get your free API key and start calculating GCD and LCM in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key