Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

# makafakachka's solution

## to Prime Factors in the Delphi Pascal Track

Published at Jan 26 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

## Example

What are the prime factors of 60?

• Our first divisor is 2. 2 goes into 60, leaving 30.
• 2 goes into 30, leaving 15.
• 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
• 3 goes cleanly into 15, leaving 5.
• 3 does not go cleanly into 5. The next possible factor is 4.
• 4 does not go cleanly into 5. The next possible factor is 5.
• 5 does go cleanly into 5.
• We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

• 2 * 2 * 3 * 5
• = 4 * 15
• = 60
• Success!

## Testing

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

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

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may receive assistance from a mentor.

### uPrimeFactorsTest.pas

``````unit uPrimeFactorsTest;

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.1.0';

type
[TestFixture]
TPrimeFactorsTest = class(TObject)
private
procedure CompareArrays(Array1, Array2: TArray<Int64>);
public
[Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure no_factors;

[Test]
[Ignore]
procedure prime_number;

[Test]
[Ignore]
procedure square_of_a_prime;

[Test]
[Ignore]
procedure cube_of_a_prime;

[Test]
[Ignore]
procedure product_of_primes_and_non_primes;

[Test]
[Ignore]
procedure product_of_primes;

[Test]
[Ignore]
procedure factors_include_a_large_prime;
end;

implementation

uses
System.SysUtils, uPrimeFactors;

procedure TPrimeFactorsTest.CompareArrays(Array1, Array2: TArray<Int64>);
var
i: integer;
begin
Assert.AreEqual(Length(Array1), Length(Array2), ' - Array lengths must be equal');
for i := Low(Array1) to High(Array1) do
Assert.AreEqual(Array1[i], Array2[i], format('Expecting element %d to = %d, Actual = %d',
[i, Array1[i], Array2[i]]));
end;

procedure TPrimeFactorsTest.cube_of_a_prime;
begin
CompareArrays([2, 2, 2], TPrimeFactors.factors(8));
end;

procedure TPrimeFactorsTest.factors_include_a_large_prime;
begin
CompareArrays([11, 9539, 894119], TPrimeFactors.factors(93819012551));
end;

procedure TPrimeFactorsTest.no_factors;
begin

CompareArrays([], TPrimeFactors.factors(1));
end;

procedure TPrimeFactorsTest.prime_number;
begin
CompareArrays([2], TPrimeFactors.factors(2));
end;

procedure TPrimeFactorsTest.product_of_primes;
begin
CompareArrays([5, 17, 23, 461], TPrimeFactors.factors(901255));
end;

procedure TPrimeFactorsTest.product_of_primes_and_non_primes;
begin
CompareArrays([2, 2, 3], TPrimeFactors.factors(12));
end;

procedure TPrimeFactorsTest.square_of_a_prime;
begin
CompareArrays([3, 3], TPrimeFactors.factors(9));
end;

initialization
TDUnitX.RegisterTestFixture(TPrimeFactorsTest);
end.``````
``````unit uPrimeFactors;

interface

type
TPrimeFactors = class
class function factors(Number: Int64): TArray<Int64>;
end;

implementation

class function TPrimeFactors.factors(Number: Int64): TArray<Int64>;
begin
SetLength(Result, 0);
if Number = 1 then Exit;

while Number mod 2 = 0 do begin
Number := Number div 2;
Result := Result + [2];
end;

var Divisor := 3;

while Number <> 1 do begin
while Number mod Divisor = 0 do begin
Number := Number div Divisor;
Result := Result + [Divisor];
end;
Divisor := Divisor + 2;
end;
end;

end.``````