🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # n0mn0m's solution

## to Perfect Numbers in the C# Track

Published at Nov 04 2020 · 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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit PerfectNumbers.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

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

### PerfectNumbersTests.cs

``````// This file was auto-generated based on version 1.1.0 of the canonical data.

using System;
using Xunit;

public class PerfectNumbersTests
{
[Fact]
public void Smallest_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(6));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Medium_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(28));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Large_perfect_number_is_classified_correctly()
{
Assert.Equal(Classification.Perfect, PerfectNumbers.Classify(33550336));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Smallest_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(12));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Medium_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(30));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Large_abundant_number_is_classified_correctly()
{
Assert.Equal(Classification.Abundant, PerfectNumbers.Classify(33550335));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Smallest_prime_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Smallest_non_prime_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(4));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Medium_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(32));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Large_deficient_number_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(33550337));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Edge_case_no_factors_other_than_itself_is_classified_correctly()
{
Assert.Equal(Classification.Deficient, PerfectNumbers.Classify(1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Zero_is_rejected_not_a_natural_number_()
{
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(0));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Negative_integer_is_rejected_not_a_natural_number_()
{
Assert.Throws<ArgumentOutOfRangeException>(() => PerfectNumbers.Classify(-1));
}
}``````
``````﻿using System;
using System.Globalization;
using System.Linq;

public enum Classification
{
Perfect,
Abundant,
Deficient
}

public static class PerfectNumbers
{
public static Classification Classify(int number)
{
if (number <= 0)
throw new ArgumentOutOfRangeException();

var aliquotSum = Enumerable.Range(1, number / 2).Where(x => number % x == 0).Sum();

if (aliquotSum == number)
return Classification.Perfect;

return aliquotSum > number ? Classification.Abundant : Classification.Deficient;
}
}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?