Sha256: e4abf17fbf995c54f05df78c4f03fb15e77ad358eef1be7581ba814b9973aa50

Contents?: true

Size: 800 Bytes

Versions: 71

Compression:

Stored size: 800 Bytes

Contents

unit uPerfectNumbers;

interface
uses System.SysUtils;

type
  ENotNaturalNumber = class(Exception);

  NumberType = (Perfect, Abundant, Deficient);

  PerfectNumber = class
  public
    class function Classify(aNumber: integer): Numbertype; static;
  end;

implementation

class function PerfectNumber.Classify(aNumber: Integer): NumberType;
var sumOfFactors: integer;
    i: integer;
begin
  if aNumber < 1 then
    raise ENotNaturalNumber.Create('Classification is only possible for natural numbers.');

  sumOfFactors := 0;

  for i := 1 to aNumber - 1 do
    if aNumber mod i = 0 then
      sumOfFactors := sumOfFactors + i;

  if sumOfFactors < aNumber then
    result := Deficient
  else
    if sumOfFactors = aNumber then
      result := Perfect
    else
      result := Abundant;
end;

end.

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.138 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.137 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.136 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.135 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.134 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.133 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.132 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.131 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.130 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.129 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.128 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.127 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.126 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.125 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.124 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.123 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.122 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.121 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.120 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas