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.119 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.118 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.117 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.116 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.115 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.114 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.113 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.111 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.110 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.109 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.108 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.107 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.106 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.105 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.104 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.103 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.102 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.101 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.100 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java
trackler-2.2.1.99 tracks/java/exercises/nth-prime/.meta/src/reference/java/PrimeCalculator.java