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

Published at Jan 21 2020
·
0 comments

Instructions

Test suite

Solution

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.

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!

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.

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

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`

.

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

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

```
unit uPrimeFactorsTests;
interface
uses
DUnitX.TestFramework;
const
CanonicalVersion = '1.1.0.1';
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(AValue:Int64): TArray<Int64>;
end;
implementation
uses
System.SysUtils, System.Generics.Collections;
{ TPrimeFactors }
class function TPrimeFactors.Factors(AValue: Int64): TArray<Int64>;
var
l:TList<Int64>;
i:Int64;
begin
Result := [];
l := TList<Int64>.Create;
try
while (AValue <> 1) do begin
for i := 2 to AValue do begin
if ((AValue mod i) = 0) then begin
l.Add(i);
Break; // not continue if founded
end;
end;
AValue := AValue div i;
end;
Result := l.ToArray;
finally
FreeAndNil(l);
end;
end;
end.
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments