Skip to content

Instantly share code, notes, and snippets.

@fhinkel
Last active December 3, 2017 19:49
Show Gist options
  • Select an option

  • Save fhinkel/2876ea53ab539c8d7c8e7026d5cdec72 to your computer and use it in GitHub Desktop.

Select an option

Save fhinkel/2876ea53ab539c8d7c8e7026d5cdec72 to your computer and use it in GitHub Desktop.

Revisions

  1. fhinkel revised this gist Dec 3, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion prime.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    function isPrime(p) {
    for(let i = 2; i <= Math.sqrt(p); i++) {
    const upper = Math.sqrt(p);
    for(let i = 2; i <= upper; i++) {
    if (p % i === 0 ) {
    return false;
    }
  2. fhinkel created this gist Dec 2, 2017.
    28 changes: 28 additions & 0 deletions prime.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    function isPrime(p) {
    for(let i = 2; i <= Math.sqrt(p); i++) {
    if (p % i === 0 ) {
    return false;
    }
    }
    return true;
    }

    // Return n-th prime
    function prime(n) {
    if (n < 1) {
    throw Error("n too small: " + n);
    }
    let count = 0;
    let result = 1;
    while(count < n) {
    result++;
    if (isPrime(result)) {
    count++;
    }
    }
    return result;
    }

    module.exports = {
    prime
    };