Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Possible Duplicate:
List of first n prime numbers most efficiently and in shortest code

This is to be closed soon - please don't answer (mod- please close!)

This challenge is to write the most efficient prime tester and nth-prime finder.

Rules

  • Entries using built in prime functions are disqualified.
  • Entries should support all integers, from 1 until 100000.
  • Use the function / method names isPrime and nthPrime.
  • Entries must be shorter than 2048 bytes.
  • Entries should be minified (but not required).
  • Points are awarded as follows:
    • 2 points if the code actually works.
    • 15 points if the code is in javascript (I'm looking for code for a JS library, but other languages are also allowed).
    • 2 points for code shorter than 256 bytes.
    • 5 points for the shortest code.
    • 2 points for the shortest code (minified) for a language.
    • 1 point if only entry for a language.
    • 3 points if a commented / formatted version is provided.
    • 3 points if supports negatives and floats (i.e., returns false for -1 or 2.03).
    • 1 point for every up-vote the answer receives.
    • 5 points for the fastest code in a language (to be judged in the case of JS by a jsPerf).
      • To be tested using 1 random number from each of the following ranges: 1-10, 11-100, 101-10000, 10001-100000.
  • The winner may get a bounty; other users can sponsor bounties too.
  • The rules may be modified (by Inkbug) at anytime.

Examples

isPrime( 0 ) -> false
isPrime( 1 ) -> false
isPrime( 1.3 ) -> false (not required)
isPrime( 2 ) -> true
isPrime( 3 ) -> true
isPrime( 4 ) -> false
nthPrime( 0 ) -> NaN
nthPrime( 1 ) -> 2
nthPrime( 1.5 ) -> NaN
nthPrime( 2 ) -> 3
share|improve this question
it is very related to your question stackoverflow.com/questions/11641098/… – Artem Ice Aug 5 '12 at 9:03
@ArtemIce I don't understand the connection. – Inkbug Aug 5 '12 at 9:27
2  
-1 There are previous questions about the first nth primes and primality testing, so this doesn't add anything new except a crazily arbitrary scoring system. – Peter Taylor Aug 5 '12 at 21:57
1  
@PeterTaylor If you point me to such questions, I'll be happy to close this. (how does one do that?) – Inkbug Aug 6 '12 at 5:08
1  
@PeterTaylor Thanks - I flagged this for closing. – Inkbug Aug 7 '12 at 7:05
show 8 more comments

marked as duplicate by primo, gnibbler Aug 10 '12 at 6:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

JavaScript, 195 bytes

Here is a very crude example answer, that should receive at least 22 points:

function isPrime(n){if(n<2)return!1;for(var i=2;i<n;i++)if(!(n%i))return!1;return!0}function nthPrime(n){var count=0,current=1;for(;count<n;){current++;if(isPrime(current))count++}return current}

Formatted version:

function isPrime ( n ) {
    if ( n < 2 ) {
        return false;
    }
    for ( var i = 2; i < n; i++ ) {
        if ( !(n % i) ) {
            return false;
        }
    }
    return true;
}
function nthPrime ( n ) {
    var count = 0;
    var current = 1;
    for ( ;count < n; ) {
        current++;
        if ( isPrime( current ) ) {
            count++;
        }
    }
    return current;
}
share|improve this answer

Clojure

Faster version:

(defn is-prime? [^long n]
  (loop [i (long 2)] 
    (if (zero? (rem n i))
      false
      (if (>= (inc i) n) true (recur (inc i))))))

(defn nth-prime [n]
  (nth (->> (range) (filter #(is-prime? %))) n))

Golf version (103):

(defn isPrime[n](every? #(>(rem n %)0)(range 2 n)))(defn nthPrime[n](nth(filter #(isPrime %)(range))n))
share|improve this answer

Python 2.7.3, 165 bytes

def isPrime(n):
    if n<2 or n<0 or isinstance(n, float):
        return False
    for x in range(2,n):
        if (n%x)==0:
            return False
    return True


def nthPrime(n):
    y=0
    z=1
    while y<n:
        z+=1
        if isPrime(z):
            y+=1
    return z
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.