{"id":17609817,"url":"https://github.com/vitaly-t/prime-lib","last_synced_at":"2025-04-30T14:30:25.261Z","repository":{"id":46284322,"uuid":"410661694","full_name":"vitaly-t/prime-lib","owner":"vitaly-t","description":"Prime number library.","archived":false,"fork":false,"pushed_at":"2022-09-19T16:08:58.000Z","size":207,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T23:06:42.016Z","etag":null,"topics":["generators","javascript","meissel-lehmer","prime-numbers","rxjs","sieve-of-eratosthenes","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vitaly-t.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-26T21:01:23.000Z","updated_at":"2024-07-02T15:35:31.000Z","dependencies_parsed_at":"2022-09-16T03:51:24.898Z","dependency_job_id":null,"html_url":"https://github.com/vitaly-t/prime-lib","commit_stats":null,"previous_names":[],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitaly-t%2Fprime-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitaly-t%2Fprime-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitaly-t%2Fprime-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitaly-t%2Fprime-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vitaly-t","download_url":"https://codeload.github.com/vitaly-t/prime-lib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251721382,"owners_count":21632827,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generators","javascript","meissel-lehmer","prime-numbers","rxjs","sieve-of-eratosthenes","typescript"],"created_at":"2024-10-22T17:10:14.958Z","updated_at":"2025-04-30T14:30:25.220Z","avatar_url":"https://github.com/vitaly-t.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"prime-lib\n---------\n\n* [About](#about)\n* [Installation](#installation)\n* [Usage](#usage)\n    * [JavaScript](#javascript)\n    * [TypeScript](#typescript)\n    * [RXJS](#rxjs)\n* [API](#api)\n\n## About\n\nPopular [prime number] functions, implemented in TypeScript, with focus on memory efficiency and performance,\nto be friendly for Web apps, and [RXJS] in particular. \n\nIt features many well-known optimization methods, such as [Sieve of Eratosthenes], [Meissel Lehmer], etc.\n\nSee the following resources:\n\n* [API](#api) - all available functions\n* [WiKi] - to help choose a prime generator\n* [Benchmarks](./benchmarks) - to see the performance\n\n## Installation\n\n```\n$ npm i prime-lib\n```\n\n## Usage\n\nFollow the usage examples below, based on your development environment.\n\n### JavaScript\n\nGenerating an array with the first 10 primes:\n\n```js\nconst {generatePrimes, stopOnCount} = require('prime-lib');\n\nconst i = generatePrimes(); // create infinite primes iterator \nconst s = stopOnCount(i, 10); // stop-iterator after 10 values\n\nconst values = [...s]; // 2, 3, 5, 7, 11, 13, 17, 19, 23, 29\n```\n\n### TypeScript\n\nGenerating an array of all primes up to 17:\n\n```js\nimport {generatePrimes, stopOnValue} from 'prime-lib';\n\nconst i = generatePrimes(); // create infinite primes iterator\nconst s = stopOnValue(i, 17); // stop-iterator when value reaches 17\n\nconst values = [...s]; // 2, 3, 5, 7, 11, 13, 17\n```\n\n### RXJS\n\nAdding a reusable [RXJS] wrapper first:\n\n```ts\nimport {Observable, from} from 'rxjs';\nimport {generatePrimes, IPrimeOptions} from 'prime-lib';\n\nexport function primes(options?: IPrimeOptions): Observable\u003cnumber\u003e {\n    return from(generatePrimes(options));\n}\n```\n\n* Generating the first 10 primes:\n\n```ts\nimport {take} from 'rxjs';\n\nprimes().pipe(take(10))\n    .subscribe(prime =\u003e {\n        // 2, 3, 5, 7, 11, 13, 17, 19, 23, 29\n    });\n```\n\n* Generating 10 primes after 100:\n\n```ts\nimport {take} from 'rxjs';\n\nprimes({start: 100}).pipe(take(10))\n    .subscribe(prime =\u003e {\n        // 101, 103, 107, 109, 113, 127, 131, 137, 139, 149\n    });\n```\n\n* Maximum-performance, buffered primes generation:\n\n```ts\nprimes({boost: 10})\n    .subscribe(prime =\u003e {\n        // 2, 3, 5, 7, 11, 13, 17, 19, 23, 29\n    });\n```\n\n* Detecting primes in another sequence:\n\n```ts\nimport {range, filter} from 'rxjs';\nimport {isPrime} from 'prime-lib';\n\nrange(0, 20).pipe(filter(a =\u003e isPrime(a)))\n    .subscribe(prime =\u003e {\n        // 2, 3, 5, 7, 11, 13, 17, 19\n    });\n```\n\n## API\n\n#### \u003ci\u003ePrime generators:\u003c/i\u003e\n\n* `generatePrimes()` - efficiency-focused, infinite prime generator\n* `generatePrimes({start: x})` - efficiency-focused, infinite prime generator, starting from `x`\n* `generatePrimes({boost: n})` - performance-focused prime generator, for up to `n` primes\n\n#### \u003ci\u003ePopular prime functions:\u003c/i\u003e\n\n* `isPrime(x)` - verifies if `x` is a prime number\n* `countPrimes(x)` - precise primes counter, up to `x`, using [Meissel Lehmer algorithm]\n* `countPrimesApprox(x)` - instant primes counter, up to `x`, with error margin \u003c 1%\n* `primeFactors(x)` - calculates prime factorization\n\nWork-in-progress:\n\n* `nthPrime(n)` - returns prime from index (_**not implemented yet**_)\n* `nthPrimeApprox(n)` - returns approximate prime from index (_**not implemented yet**_)\n\n#### \u003ci\u003eExtra helpers:\u003c/i\u003e\n\n* `cachePrimes(n)` - creates a gap-compressed cache of `n` primes (see [WiKi]) \n* `stopOnCount(iterator, count)` - stops the iterator, once `count` has been reached\n* `stopOnValue(iterator, maxValue)` - stops the iterator when `maxValue` is exceeded\n* `stopWhen(iterator, cb)` - stops the iterator when the callback returns a truthy value\n\n[prime number]:https://en.wikipedia.org/wiki/Prime_number\n\n[Sieve of Eratosthenes]:https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\n\n[WiKi]:https://github.com/vitaly-t/prime-lib/wiki\n\n[RXJS]:https://github.com/ReactiveX/rxjs\n\n[Meissel Lehmer]:https://en.wikipedia.org/wiki/Meissel%E2%80%93Lehmer_algorithm\n[Meissel Lehmer algorithm]:https://en.wikipedia.org/wiki/Meissel%E2%80%93Lehmer_algorithm\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitaly-t%2Fprime-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitaly-t%2Fprime-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitaly-t%2Fprime-lib/lists"}