lots of exercises in java... from https://github.com/exercism/java

NaturalNumber.java 843B

12345678910111213141516171819202122232425262728293031
  1. import java.util.stream.IntStream;
  2. final class NaturalNumber {
  3. private final int naturalNumber;
  4. NaturalNumber(int naturalNumber) {
  5. if (naturalNumber <= 0) throw new IllegalArgumentException("You must supply a natural number (positive integer)");
  6. this.naturalNumber = naturalNumber;
  7. }
  8. Classification getClassification() {
  9. final int aliquotSum = computeAliquotSum();
  10. if (aliquotSum == naturalNumber) {
  11. return Classification.PERFECT;
  12. } else if (aliquotSum > naturalNumber) {
  13. return Classification.ABUNDANT;
  14. } else {
  15. return Classification.DEFICIENT;
  16. }
  17. }
  18. private int computeAliquotSum() {
  19. return IntStream.range(1, naturalNumber)
  20. .filter(it -> naturalNumber % it == 0)
  21. .sum();
  22. }
  23. }