Need to find all prime numbers between 100 and 200? Or primes in any numeric range? The Primes in Range API uses optimized sieve algorithms to efficiently find every prime within your specified bounds.
Finding Primes in a Range
Finding all primes in a range is a common mathematical task. While you could check each number individually for primality, sieve-based algorithms are far more efficient for generating lists of primes.
For example, there are 21 primes between 100 and 200: 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199.
Sieve Algorithms
The API uses efficient sieving techniques:
Sieve of Eratosthenes
Classic algorithm marking multiples of each prime as composite. Efficient for smaller ranges.
Segmented Sieve
Memory-efficient variant for large ranges, processing in segments.
Prime Counting
The API also provides pi(x) - the count of primes up to x - without listing them all.
Using the Primes in Range API
TinyFn provides an efficient endpoint to find primes in any range:
GET https://api.tinyfn.io/v1/math/primes-in-range?start=100&end=200
Headers: X-API-Key: your-api-key
{
"start": 100,
"end": 200,
"primes": [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199],
"count": 21,
"first_prime": 101,
"last_prime": 199
}
Parameters
| Parameter | Type | Description |
|---|---|---|
start |
integer | Start of range (required, minimum 2) |
end |
integer | End of range (required, maximum 10^9) |
count_only |
boolean | Return only count, not full list (default: false) |
limit |
integer | Maximum primes to return (default: 10000) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/math/primes-in-range?start=1&end=100',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const data = await response.json();
console.log(`Found ${data.count} primes between 1 and 100`);
console.log(`Primes: ${data.primes.join(', ')}`);
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/math/primes-in-range',
headers={'X-API-Key': 'your-api-key'},
params={'start': 1000, 'end': 2000}
)
data = response.json()
print(f"Primes between 1000-2000: {data['count']}")
print(f"First: {data['first_prime']}, Last: {data['last_prime']}")
cURL
curl "https://api.tinyfn.io/v1/math/primes-in-range?start=100&end=200" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Cryptography: Find primes of specific sizes for key generation
- Educational Tools: Visualize prime distribution
- Algorithm Testing: Generate prime test data
- Prime Gap Analysis: Study distances between consecutive primes
- Mathematical Research: Investigate prime patterns and conjectures
Best Practices
- Use count_only for large ranges: When you need only the count, not the list
- Set reasonable limits: Large ranges can return millions of primes
- Page through results: For huge ranges, make multiple requests
- Consider memory: Very large prime lists require significant memory
- Cache common ranges: Primes do not change, so results can be cached
Try the Primes in Range API
Get your free API key and start finding prime numbers in any range.
Get Free API Key