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

## to Perfect Numbers in the C# Track

Published at Apr 08 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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

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

### PerfectNumbersTest.cs

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

using System;
using Xunit;

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

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

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

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

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

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

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

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

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

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

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

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

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

public enum Classification
{
Perfect,
Abundant,
Deficient
}

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

int total = 0;

for(int i = 1; i < number; i++)
{
if(number % i == 0) total += i;
}

if(total == number)
return Classification.Perfect;
else if(total > number)
return Classification.Abundant;
else
return Classification.Deficient;
}
}``````