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.159 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.158 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.157 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.156 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.155 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.154 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.153 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.152 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.151 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.150 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.149 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.148 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.147 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.146 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.145 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.144 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.143 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.142 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.141 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas
trackler-2.2.1.140 tracks/delphi/exercises/perfect-numbers/uPerfectNumbersExample.pas