Sha256: 6cd5edcb5303f025469cdfc2281ed998e5868ffbe21c73ef0bc0a344da1337a0

Contents?: true

Size: 846 Bytes

Versions: 13

Compression:

Stored size: 846 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);

        if (divisible) {
            return false;
        }

        return true;
    }
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
trackler-2.0.8.54 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.53 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.52 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.51 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.50 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.49 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.48 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.47 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.46 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.45 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.44 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.43 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java
trackler-2.0.8.42 tracks/java/exercises/nth-prime/src/example/java/PrimeCalculator.java