The Eratosthenes Sieve Method Implemented in TypeScript
  • TypeScript 94.8%
  • JavaScript 5.2%
Find a file
2026-05-11 23:00:39 +08:00
.github/workflows Add GitHub Actions workflow for testing 2026-05-11 22:39:54 +08:00
src Implement Sieve of Eratosthenes function 2026-05-11 22:35:28 +08:00
tests Add unit tests for sieveOfEratosthenes function 2026-05-11 22:38:46 +08:00
.gitignore Add .gitignore to exclude node_modules and build files 2026-05-11 22:39:25 +08:00
jest.config.js Add Jest configuration for testing 2026-05-11 23:00:39 +08:00
LICENSE Initial commit 2026-05-11 22:20:37 +08:00
package-lock.json add package-lock.json for CI 2026-05-11 22:58:20 +08:00
package.json Initialize package.json for TypeScript project 2026-05-11 22:36:26 +08:00
README.md Enhance README with installation and usage details 2026-05-11 22:43:49 +08:00
tsconfig.json Add initial TypeScript configuration file 2026-05-11 22:37:24 +08:00

Sieve of Eratosthenes (TypeScript)

A readable, zero-dependency implementation of the Sieve of Eratosthenes in TypeScript.
Generate all prime numbers up to a given limit with classic algorithmic efficiency.

Installation

npm install sieve-of-eratosthenes-ts

Usage

Import the function and call it with any positive integer limit:

import { sieveOfEratosthenes } from 'sieve-of-eratosthenes-ts';

console.log(sieveOfEratosthenes(100));
// [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

If the full name feels too verbose, use a local import alias:

import { sieveOfEratosthenes as sieve } from 'sieve-of-eratosthenes-ts';
console.log(sieve(100));

The package exports only one function, so you stay in full control over naming.

Algorithm

The Sieve of Eratosthenes marks multiples of each prime starting from 2, leaving only primes behind.
It runs in O(n log log n) time and uses O(n) space.

Metric Value
Time complexity O(n log log n)
Space complexity O(n)
Limit support Up to ~10⁷10⁸ in Node (memorybound)

For larger limits, a segmented sieve is recommended.

Testing

npm test

Tests cover edge cases (limit < 2) and verify correct prime sets up to 100.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT © 2026 0xA672

See Also

For a highperformance Rust equivalent, check out primal a stateoftheart prime sieve with estimation and factorisation, maintained by Huon Wilson, former Rust core team member.