Sha256: e0b339c49a94b7c21d13bdbf62812c1789b45e299c7b47b1430a62f536bad8f5

Contents?: true

Size: 822 Bytes

Versions: 24

Compression:

Stored size: 822 Bytes

Contents

import java.util.stream.IntStream;

final class NaturalNumber {

    private final int naturalNumber;

    NaturalNumber(int naturalNumber) {
        if (naturalNumber <= 0) throw new IllegalStateException("Natural numbers are all positive.");
        this.naturalNumber = naturalNumber;
    }

    Classification getClassification() {
        final int aliquotSum = computeAliquotSum();

        if (aliquotSum == naturalNumber) {
            return Classification.PERFECT;
        } else if (aliquotSum > naturalNumber) {
            return Classification.ABUNDANT;
        } else {
            return Classification.DEFICIENT;
        }
    }

    private int computeAliquotSum() {
        return IntStream.range(1, naturalNumber)
                .filter(it -> naturalNumber % it == 0)
                .sum();
    }

}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
trackler-2.0.8.11 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.10 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.9 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.8 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.7 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.6 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.5 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.4 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.3 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.2 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.8.1 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.7.0 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.44 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.43 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.42 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.41 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.40 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.39 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.38 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java
trackler-2.0.6.37 tracks/java/exercises/perfect-numbers/src/example/java/NaturalNumber.java