 # Albert-Wang-Hipmunk's solution

## to Perfect Numbers in the Kotlin Track

Published at Oct 18 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 Kotlin to install the necessary dependencies:

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

## Making the test suite pass

Execute the tests with:

``````\$ gradlew 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` 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.kt

``````import org.junit.Ignore
import org.junit.Test

import org.junit.Assert.assertEquals

class NaturalNumberTest {

@Test
fun smallPerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, classify(6))
}

@Ignore
@Test
fun mediumPerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, classify(28))
}

@Ignore
@Test
fun largePerfectNumberIsClassifiedCorrectly() {
assertEquals(Classification.PERFECT, classify(33550336))
}

@Ignore
@Test
fun smallAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, classify(12))
}

@Ignore
@Test
fun mediumAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, classify(30))
}

@Ignore
@Test
fun largeAbundantNumberIsClassifiedCorrectly() {
assertEquals(Classification.ABUNDANT, classify(33550335))
}

@Ignore
@Test
fun smallestPrimeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, classify(2))
}

@Ignore
@Test
fun smallestNonPrimeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, classify(4))
}

@Ignore
@Test
fun mediumNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, classify(32))
}

@Ignore
@Test
fun largeDeficientNumberIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, classify(33550337))
}

@Ignore
@Test
fun edgeCaseWithNoFactorsOtherThanItselfIsClassifiedCorrectly() {
assertEquals(Classification.DEFICIENT, classify(1))
}

@Ignore
@Test(expected = RuntimeException::class)
fun zeroIsNotANaturalNumber() {
classify(0)
}

@Ignore
@Test(expected = RuntimeException::class)
fun negativeNumberIsNotANaturalNumber() {
classify(-1)
}

}``````
``````enum class Classification {
DEFICIENT, PERFECT, ABUNDANT
}

fun main(args: Array<String>) {
println(getAllFactors(6))
}

fun classify(naturalNumber: Int): Classification {
val sum = getAllFactors(naturalNumber).sum()
return when {
sum == naturalNumber -> Classification.PERFECT
sum > naturalNumber -> Classification.ABUNDANT
else -> Classification.DEFICIENT
}
}

private fun getAllFactors(num: Int): Set<Int> {
require(num > 0) {"num should be at least 1"}
val result = mutableSetOf<Int>()
var upperNumMin = num
for (i in 1 .. num) {
if (num % i == 0) {
}
if (num > upperNumMin) break
}
result.remove(num)
return result
}``````