Sha256: 602e12466773cb652e31d457536d321c4fcf41fe9d380a35a24bf68dc0dafb29

Contents?: true

Size: 589 Bytes

Versions: 168

Compression:

Stored size: 589 Bytes

Contents

class NthPrime {

  private static def isPrime(int n) {
    def sqrt = Math.ceil(Math.sqrt(n))
    for (int i = 3; i <= sqrt; i += 1 ) {
      if ( n % i == 0 ) {
        return false
      }
    }
    return true
  }

  static nth(int n) {
    // handle stuff that doesn't need
    // additional calculations
    if ( n < 1 ) throw new ArithmeticException()
    if ( n == 1 ) return 2

    def lastValue = 0
    def count = 1
    def i = 3

    while ( count < n ) {
      if ( isPrime(i) ) {
        lastValue = i
        count += 1
      }
      i += 2
    }
    return lastValue
  }
}

Version data entries

168 entries across 168 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.179 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.178 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.177 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.176 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.175 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.174 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.173 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.172 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.171 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.170 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.169 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.167 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.166 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.165 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.164 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.163 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.162 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.161 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.160 tracks/groovy/exercises/nth-prime/Example.groovy