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