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.78 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.77 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.76 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.75 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.74 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.73 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.72 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java