Sha256: ada9a76e8256fb848454acf105611491e5df360be8f5802cae628b4ade778fbe

Contents?: true

Size: 776 Bytes

Versions: 107

Compression:

Stored size: 776 Bytes

Contents

import java.util.stream.IntStream;

final class PrimeCalculator {
    int nth(int nth) {
        if (nth < 1) {
            throw new IllegalArgumentException();
        }

        int primesFound = 0;
        int possiblePrime = 1;

        while (primesFound < nth) {
            possiblePrime++;

            if (isPrime(possiblePrime)) {
                primesFound++;
            }
        }

        return possiblePrime;
    }

    private boolean isPrime(int n) {
        if (n == 1) {
            return false;
        }

        if (n == 2) {
            return true;
        }

        boolean divisible = IntStream
                .rangeClosed(2, (int) Math.ceil(Math.sqrt(n)))
                .anyMatch((int i) -> n % i == 0);

        return !divisible;
    }
}

Version data entries

107 entries across 107 versions & 1 rubygems

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