Sha256: 752a24630698ef0c0f8d98c94d89dc05bb8d4f88df8643582d9ec819e4ecdfb0

Contents?: true

Size: 586 Bytes

Versions: 155

Compression:

Stored size: 586 Bytes

Contents

class Prime {

  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

155 entries across 155 versions & 1 rubygems

Version Path
trackler-2.2.1.10 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.9 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.8 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.7 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.6 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.5 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.4 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.3 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.2 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.1 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.1.0 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.6 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.5 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.4 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.3 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.2 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.1 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.2.0.0 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.1.0.55 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.1.0.54 tracks/groovy/exercises/nth-prime/Example.groovy