Nth Prime Number API: Find Any Prime in the Sequence

What is the 1000th prime number? The millionth? The Nth Prime API finds any prime number by its position in the infinite sequence of primes, instantly returning results for positions up to several million.

What are Prime Numbers?

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29...

Prime numbers are fundamental to number theory and have crucial applications in cryptography, where large primes are used in encryption algorithms.

The Prime Number Sequence

Notable primes in the sequence:

Position (n) Prime Number
12
1029
100541
1,0007,919
10,000104,729
100,0001,299,709
1,000,00015,485,863
Fun Fact: The Prime Number Theorem states that the nth prime is approximately n * ln(n) for large n. This helps estimate where to look for large primes.

Using the Nth Prime API

TinyFn provides a fast endpoint to find any prime by position:

API Request
GET https://api.tinyfn.io/v1/math/nth-prime?n=1000
Headers: X-API-Key: your-api-key
Response
{
  "n": 1000,
  "prime": 7919,
  "previous_prime": 7907,
  "next_prime": 7927,
  "digit_count": 4
}

Parameters

Parameter Type Description
n integer Position in prime sequence (required, 1 to 10,000,000)
include_neighbors boolean Include previous and next primes (default: true)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/math/nth-prime?n=10000',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`The 10,000th prime is ${data.prime}`); // 104729

Python

import requests

response = requests.get(
    'https://api.tinyfn.io/v1/math/nth-prime',
    headers={'X-API-Key': 'your-api-key'},
    params={'n': 1000000}
)
data = response.json()
print(f"The millionth prime is: {data['prime']:,}")  # 15,485,863

cURL

curl "https://api.tinyfn.io/v1/math/nth-prime?n=100" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Cryptography: Generate prime numbers for encryption keys
  • Educational Tools: Teach about prime numbers and sequences
  • Math Challenges: Project Euler and similar programming puzzles
  • Random Prime Selection: Pick primes at specific positions
  • Prime Verification: Check position of known primes

Best Practices

  1. Cache frequent results: Common positions like 100, 1000 are often requested
  2. Use batching: Request ranges with the primes-in-range endpoint when needed
  3. Consider approximations: For very large n, prime estimates may suffice
  4. Handle limits: Very large positions may exceed API limits
  5. Verify for cryptography: For security-critical primes, use additional verification

Try the Nth Prime API

Get your free API key and start finding prime numbers.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key