Sha256: 515af16dbc96fa650397dc4905a8f647266379fe8d5eb94c6e6ef1e9b68852b2

Contents?: true

Size: 632 Bytes

Versions: 73

Compression:

Stored size: 632 Bytes

Contents

import groovy.transform.CompileStatic

@CompileStatic
class Prime {

  private 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
  }

  def 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

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.0.6.17 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.16 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.15 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.14 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.13 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.12 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.11 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.10 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.9 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.8 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.7 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.6 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.5 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.4 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.3 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.2 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.1 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.6.0 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.5.18 tracks/groovy/exercises/nth-prime/Example.groovy
trackler-2.0.5.17 tracks/groovy/exercises/nth-prime/Example.groovy