Sha256: 3fa47b354520f0c3483b406002c5864b08451bfe3c399668c80cee84dbbb1516

Contents?: true

Size: 790 Bytes

Versions: 135

Compression:

Stored size: 790 Bytes

Contents

import java.util.stream.IntStream;

public final class PrimeCalculator {
    public 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

135 entries across 135 versions & 1 rubygems

Version Path
trackler-2.2.1.71 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.70 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.69 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.68 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.67 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.66 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.65 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.64 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.63 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.62 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.61 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.60 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.59 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.58 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.57 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.56 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.55 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.54 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.53 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.52 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java