Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of michkowalczuk

michkowalczuk's solution

to Perfect Numbers in the Delphi Pascal Track

Published at Sep 12 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.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

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 may request help from a mentor.

uPerfectNumbersTests.pas

unit uPerfectNumbersTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.1.0.1';

type

  [TestFixture]
  PerfectNumbersTest = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Smallest_perfect_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Medium_perfect_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Large_perfect_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Smallest_abundant_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Medium_abundant_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Large_abundant_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Smallest_prime_deficient_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Smallest_non_prime_deficient_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Medium_deficient_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Large_deficient_number_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Edge_case_no_factors_other_than_itself_is_classified_correctly;

    [Test]
    [Ignore]
    procedure Zero_is_rejected_not_a_natural_number;

    [Test]
    [Ignore]
    procedure Negative_integer_is_rejected_not_a_natural_number;
  end;

implementation
uses uPerfectNumbers;

procedure PerfectNumbersTest.Smallest_prime_deficient_number_is_classified_correctly;
begin
  Assert.AreEqual(Deficient, PerfectNumber.Classify(2));
end;

procedure PerfectNumbersTest.Smallest_non_prime_deficient_number_is_classified_correctly;
begin
  Assert.AreEqual(Deficient, PerfectNumber.Classify(4));
end;

procedure PerfectNumbersTest.Medium_deficient_number_is_classified_correctly;
begin
  Assert.AreEqual(Deficient, PerfectNumber.Classify(32));
end;

procedure PerfectNumbersTest.Large_deficient_number_is_classified_correctly;
begin
  Assert.AreEqual(Deficient, PerfectNumber.Classify(33550337));
end;

procedure PerfectNumbersTest.Edge_case_no_factors_other_than_itself_is_classified_correctly;
begin
  Assert.AreEqual(Deficient, PerfectNumber.Classify(1));
end;

procedure PerfectNumbersTest.Smallest_perfect_number_is_classified_correctly;
begin
  Assert.AreEqual(Perfect, PerfectNumber.Classify(6));
end;

procedure PerfectNumbersTest.Medium_perfect_number_is_classified_correctly;
begin
  Assert.AreEqual(Perfect, PerfectNumber.Classify(28));
end;

procedure PerfectNumbersTest.Large_perfect_number_is_classified_correctly;
begin
  Assert.AreEqual(Perfect, PerfectNumber.Classify(33550336));
end;

procedure PerfectNumbersTest.Smallest_abundant_number_is_classified_correctly;
begin
  Assert.AreEqual(Abundant, PerfectNumber.Classify(12));
end;

procedure PerfectNumbersTest.Medium_abundant_number_is_classified_correctly;
begin
  Assert.AreEqual(Abundant, PerfectNumber.Classify(30));
end;

procedure PerfectNumbersTest.Large_abundant_number_is_classified_correctly;
begin
  Assert.AreEqual(Abundant, PerfectNumber.Classify(33550335));
end;

procedure PerfectNumbersTest.Zero_is_rejected_not_a_natural_number;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              PerfectNumber.Classify(0);
            end;

  assert.WillRaiseWithMessage(MyProc, ENotNaturalNumber, 'Classification is only possible for natural numbers.');
end;

procedure PerfectNumbersTest.Negative_integer_is_rejected_not_a_natural_number;
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              PerfectNumber.Classify(-1);
            end;

  assert.WillRaiseWithMessage(MyProc, ENotNaturalNumber, 'Classification is only possible for natural numbers.');
end;

initialization
  TDUnitX.RegisterTestFixture(PerfectNumbersTest);
end.
unit uPerfectNumbers;

interface
uses
  System.Sysutils;

  type
  TCategory = (Deficient, Perfect, Abundant);
  /// <summary>
  ///   Class for performing classification of numbers based on Nicomachus'\
  ///   (60 - 120 CE) classification scheme for natural numbers.
  /// </summary>
  PerfectNumber = class
    public
      /// <summary>
      ///   Determine if a number is perfect, abundant, or deficient.
      /// </summary>
      /// <param name="number">
      ///   given natural number
      /// </param>
      /// <returns>
      ///   category of the number
      /// </returns>
      class function Classify(const number : Integer) : TCategory;
  end;

  ENotNaturalNumber = class( Exception ) ;

implementation

uses
  System.Math;

class function PerfectNumber.Classify(const number : Integer) : TCategory;
var
  i : Integer ;
  aliquot_sum : Integer ;
begin
  aliquot_sum := 0 ;

  if number <= 0 then
    raise ENotNaturalNumber.Create(
      'Classification is only possible for natural numbers.');

  if number <= 2 then
    Exit(Deficient) ;

  for i := 1 to (number div 2) do
    if number mod i = 0 then
      Inc(aliquot_sum, i);

  if aliquot_sum < number then
    Result := Deficient
  else if aliquot_sum = number then
    Result := Perfect
  else
    Result := Abundant;
end;

end.

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?