{"id":15355815,"url":"https://github.com/fibo/prime-number","last_synced_at":"2025-04-15T06:39:04.754Z","repository":{"id":57330329,"uuid":"54738778","full_name":"fibo/prime-number","owner":"fibo","description":"is a recursive function to check if a number is prime (and a benchmark to test how slow it is :)","archived":false,"fork":false,"pushed_at":"2020-03-16T20:50:31.000Z","size":19,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-18T05:03:17.819Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://fibo.github.io/prime-number","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fibo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-25T18:28:42.000Z","updated_at":"2024-05-10T11:37:16.000Z","dependencies_parsed_at":"2022-09-21T02:21:45.200Z","dependency_job_id":null,"html_url":"https://github.com/fibo/prime-number","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fprime-number","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fprime-number/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fprime-number/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fprime-number/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibo","download_url":"https://codeload.github.com/fibo/prime-number/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240966417,"owners_count":19886071,"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":[],"created_at":"2024-10-01T12:25:44.143Z","updated_at":"2025-02-27T02:31:25.427Z","avatar_url":"https://github.com/fibo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# prime-number\n\n\u003e is a recursive function to check if a number is prime (and a benchmark to test how slow it is :)\n\n[![KLP](https://img.shields.io/badge/kiss-literate-orange.svg)](https://github.com/fibo/kiss-literate-programming)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\n**Table of Contents**\n\n* [Usage](#usage): `isPrime`, how to benchmark, primes list.\n* [Installation](#installation): with npm or copy and paste.\n* [Source](#source): embedded in this file.\n* [License](#license): MIT.\n\n[Is it 1 a prime?](https://en.wikipedia.org/wiki/Prime_number#Primality_of_one)\nSome years ago I composed a djembe rhythm based on prime numbers, and it sounds better if 1 is considered prime. Casually, the algorithm implemented here defines 1 as a *not prime*.\n\n## Usage\n\nAs you might expect, you can do\n\n```js\nconst isPrime = require('prime-number')\n\nconsole.log(isPrime(19)) // true\n```\n\nThere is a list of few primes available, if you want to use it\n\n```js\nconst primeNumberList = require('prime-number/list')\nconsole.log(primeNumberList)\n```\n\nYou can benchmark other primality check algorithms.\n\n```js\nconst isPrime = require('yet-another-primality-check')\nconst benchmark = require('prime-number/benchmark')\nconst from = 1000\nconst to = Number.MAX_SAFE_INTEGER\n\nbenchmark(isPrime)(from, to)\n```\n\nUsing a oneliner, let's check few primality check packages found on npm.\n\n```bash\n# node -e \"require('prime-number/benchmark')(require('prime-number'))(10000, 100000)\"\nFound 8363 primes\nPrimality benchmark: 44.703s\n\n# node -e \"require('prime-number/benchmark')(require('is-prime'))(10000, 100000)\"\nFound 8363 primes\nPrimality benchmark: 14.885ms\n\n# node -e \"require('prime-number/benchmark')(require('check-prime'))(10000, 100000)\"\nFound 8363 primes\nprimality benchmark: 61.613ms\n```\n\nSo I can state that\n\n\u003e My algorithm sucks! 🐸\n\n## Installation\n\nWith [npm](https://npmjs.org/) do\n\n```bash\nnpm install prime-number\n```\n\nOr copy and paste the code below.\n\n## Source\n\nFirst of all, do not check if **num** is an integer or positive or less than `Number.MAX_SAFE_INTEGER`.\nYou can do it with some other function before calling `primeNumber`.\n\n```javascript\n// Remember if a number is prime.\nconst memoize = { isPrime: {}, isNotPrime: {} }\nmemoize.isNotPrime[1] = true\nmemoize.isPrime[2] = true\n\n/**\n * Check if a number is prime.\n *\n * @param {Number}\n *\n * @returns {Boolean}\n */\n\nfunction primeNumber (num) {\n  // Avoid computing twice.\n  if (memoize.isPrime[num]) return true\n  if (memoize.isNotPrime[num]) return false\n\n  const knowPrimes = Object.keys(memoize.isPrime)\n\n  for (let i = 0; i \u003c knowPrimes.length; i++) {\n    const p = Number(knowPrimes[i])\n\n    if (num === p) return true\n\n    if (num % p === 0) {\n      memoize.isNotPrime[num] = true\n      return false\n    }\n  }\n\n  for (\n    let i = 3;\n    // Do not excede the square root of num.\n    i * i \u003c= num;\n    // All prime numbers are 1 or 5 modulo 6.\n    // Since we start with 3, this will do: 3 -\u003e 5 -\u003e 7 -\u003e 11 ... +2 -\u003e +4 -\u003e +2 -\u003e +4 ...\n    i = i % 6 === 1 ? i + 4 : i + 2\n  ) {\n    if (primeNumber(i)) { // \u003c-- Recursion here!\n      if (num % i === 0) {\n        memoize.isNotPrime[num] = true\n        return false\n      }\n    }\n  }\n\n  memoize.isPrime[num] = true\n  return true\n}\n```\n\nExport `primeNumber` function\n\n```javascript\nmodule.exports = primeNumber\n```\n\n## License\n\n[MIT](http://g14n.info/mit-license/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fprime-number","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibo%2Fprime-number","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fprime-number/lists"}