 # 230695's solution

## to Perfect Numbers in the Java Track

Published at Oct 09 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

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

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## 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);
}

}``````
``````class NaturalNumber {

private int number;
private int sum ;

public NaturalNumber(int number) {
this.number = number;

if (number <= 0) {
throw new IllegalArgumentException("You must supply a natural number (positive integer)");
}
}

public Object getClassification() {

for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
System.out.println(sum);
}
}

if (sum == number) {
return Classification.PERFECT;
} else if (sum < number) {
return Classification.DEFICIENT;
} else {
return Classification.ABUNDANT;
}
}
}``````