Overview

The Prime Generator API provides lightning-fast access to a pre-computed database of prime numbers. Get any prime by its index position with sub-10ms response times. The database grows dynamically as more primes are computed.

🚀 Lightning Fast

Database-driven lookups with intelligent caching - 1-8ms response times

✨ Simple

Clean JSON responses with just the index and prime number

🔗 RESTful

Simple HTTP GET requests with JSON responses

Endpoints

GET /prime-generator/api?pi={index}

Get the nth prime number (1-indexed) from our 10 billion prime database.

Parameters

Parameter Type Required Description
pi integer Yes Prime index (1 to current maximum). Use /stats endpoint to check current limit. Example: pi=1 returns 2 (first prime)

Example Requests

GET https://vincentmossman.com/prime-generator/api?pi=100
GET https://vincentmossman.com/prime-generator/api?pi=1000000
GET https://vincentmossman.com/prime-generator/api?pi=1000000000

Example Responses

// 100th prime
{
  "index": 100,
  "prime": 541
}

// 1 millionth prime
{
  "index": 1000000,
  "prime": 15485863
}

// 1 billionth prime
{
  "index": 1000000000,
  "prime": 22801763489
}

Response Fields

Field Type Description
index integer The requested prime index
prime integer The prime number at the given index

Database Stats Endpoint

GET /prime-generator/stats

Get current database statistics including maximum available prime index and build progress.

Example Response

{
  "database": {
    "max_prime_index": 55000000,
    "total_segments": 55,
    "status": "in_progress",
    "target_count": 10000000000
  },
  "cache": {
    "cacheSize": 5,
    "maxCacheSize": 100
  },
  "timestamp": "2025-08-05T15:30:45.123Z",
  "version": "2025-08-05-dynamic"
}

Error Responses

Invalid Index (400 Bad Request)

{
  "error": "Invalid prime index",
  "message": "Index must be between 1 and 55,000,000",
  "current_max": 55000000,
  "requested": 100000000,
  "suggestion": "Try a smaller index"
  "example": "/prime-generator/api?pi=5"
}

Rate Limit Exceeded (429 Too Many Requests)

{
  "error": "Server busy - rate limit exceeded",
  "message": "Too many requests. Please try again in 1 second.",
  "limit": 5,
  "window": "1 second",
  "suggestion": "For bulk requests, add delays between calls"
}

Database Lookup Failed (500 Internal Server Error)

{
  "error": "Prime lookup failed",
  "message": "Unable to retrieve the requested prime from database",
  "index": 15000000000
}

API Testing

Test the API directly from this page. Enter a prime index and see the live response.

Loading current limit...
Request URL: https://vincentmossman.com/prime-generator/api?pi=10
Response: Ready
Click "Test API" to see the response

Usage Examples

JavaScript (Fetch)

// Get the 1 millionth prime
fetch('https://vincentmossman.com/prime-generator/api?pi=1000000')
  .then(response => response.json())
  .then(data => {
    console.log(\`The ${data.index.toLocaleString()}th prime is: ${data.prime.toLocaleString()}\`);
    // Output: "The 1,000,000th prime is: 15,485,863"
  })
  .catch(err => console.error('API Error:', err));

Python (Requests)

import requests
import time

# Test large prime lookup performance
start = time.time()
response = requests.get('https://vincentmossman.com/prime-generator/api?pi=500000000')
data = response.json()
end = time.time()

print(f"The {data['index']:,}th prime is: {data['prime']:,}")
print(f"Network + lookup time: {(end-start)*1000:.1f}ms")
# Typical output: "Network + lookup time: 45.2ms"

cURL

# Get a large prime quickly
curl "https://vincentmossman.com/prime-generator/api?pi=100000000" \
  -H "Accept: application/json" \
  -w "Response time: %{time_total}s\n"

# Batch multiple requests
for i in 1000000 10000000 100000000; do
  echo "Prime #$i:"
  curl -s "https://vincentmossman.com/prime-generator/api?pi=$i" | jq
done

Rate Limiting & Performance

  • Rate Limit: 5 API requests per second per IP address (429 error if exceeded)
  • Database: Pre-computed 10 billion primes with intelligent segment caching
  • Max Index: Dynamic based on database build progress - check /stats endpoint for current limit
  • Response Time: 1-8ms database lookup + network latency (typically under 50ms total)
  • Concurrency: Handles multiple simultaneous requests efficiently