rootulp's solution

to Perfect Numbers in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematician Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

• Perfect: aliquot sum = number
• 6 is a perfect number because (1 + 2 + 3) = 6
• 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
• Abundant: aliquot sum > number
• 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
• 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
• Deficient: aliquot sum < number
• 8 is a deficient number because (1 + 2 + 4) = 7
• Prime numbers are deficient

Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.

Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. http://shop.oreilly.com/product/0636920029687.do

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

NaturalNumberTest.java

``````import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class NaturalNumberTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testSmallPerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, new NaturalNumber(6).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testMediumPerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, new NaturalNumber(28).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testLargePerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, new NaturalNumber(33550336).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testSmallAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, new NaturalNumber(12).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testMediumAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, new NaturalNumber(30).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testLargeAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, new NaturalNumber(33550335).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testSmallestPrimeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, new NaturalNumber(2).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testSmallestNonPrimeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, new NaturalNumber(4).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testMediumDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, new NaturalNumber(32).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testLargeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, new NaturalNumber(33550337).getClassification());
}

@Ignore("Remove to run test")
@Test
/*
* The number 1 has no proper divisors (https://en.wikipedia.org/wiki/Divisor#Further_notions_and_facts), and the
* additive identity is 0, so the aliquot sum of 1 should be 0. Hence 1 should be classified as deficient.
*/
public void testThatOneIsCorrectlyClassifiedAsDeficient() {
assertEquals(Classification.DEFICIENT, new NaturalNumber(1).getClassification());
}

@Ignore("Remove to run test")
@Test
public void testThatNonNegativeIntegerIsRejected() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You must supply a natural number (positive integer)");

new NaturalNumber(0);
}

@Ignore("Remove to run test")
@Test
public void testThatNegativeIntegerIsRejected() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You must supply a natural number (positive integer)");

new NaturalNumber(-1);
}

}``````
``````import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class NaturalNumber {

private int naturalNumber;

public NaturalNumber(int naturalNumber) {
if (invalidNaturalNumber(naturalNumber)) {
throw new IllegalArgumentException("You must supply a natural number (positive integer)",
new Throwable(Integer.toString(naturalNumber)));
}
this.naturalNumber = naturalNumber;
}

public Classification getClassification() {
int aliquotSum = aliquotSum(naturalNumber);

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

private int aliquotSum(int number) {
return factorsExcludingNumber(number).stream().mapToInt(Integer::intValue).sum();
}

private Set<Integer> factorsExcludingNumber(int number) {
Set<Integer> factorsExcludingNumber = factorsOf(number);
factorsExcludingNumber.remove(number);
return factorsExcludingNumber;
}

private Set<Integer> factorsOf(int number) {
Set<Integer> factors = new HashSet<Integer>();
for (int potentialFactor = 1; potentialFactor < Math.ceil(Math.sqrt(number)); potentialFactor += 1) {
if (number % potentialFactor == 0) {
}
}
return factors;
}

private boolean invalidNaturalNumber(int number) {
return number <= 0;
}

}``````