ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# Greycoat21's solution

## to Nth Prime in the Delphi Pascal Track

Published at Mar 15 2020 · 0 comments
Instructions
Test suite
Solution

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.

## 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

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

## Submitting Incomplete Solutions

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

### uNthPrimeTests.pas

``````unit uNthPrimeTests;

interface

uses
DUnitX.TestFramework;

const
CanonicalVersion = '2.1.0.1';

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

[Test]
[Ignore]
procedure first_prime;

[Test]
[Ignore]
procedure second_prime;

[Test]
[Ignore]
procedure sixth_prime;

[Test]
[Ignore]
procedure big_prime;
end;

implementation

uses
System.SysUtils, uNthPrime;

procedure TNthPrimeTest.first_prime;
begin
Assert.AreEqual(2, NthPrime(1));
end;

procedure TNthPrimeTest.second_prime;
begin
Assert.AreEqual(3, NthPrime(2));
end;

procedure TNthPrimeTest.sixth_prime;
begin
Assert.AreEqual(13, NthPrime(6));
end;

procedure TNthPrimeTest.big_prime;
begin
Assert.AreEqual(104743, NthPrime(10001));
end;

procedure TNthPrimeTest.there_is_no_zeroth_prime;
begin
Assert.WillRaiseWithMessage(procedure begin NthPrime(0); end , EArgumentOutOfRangeException, 'there is no zeroth prime');
end;

initialization
TDUnitX.RegisterTestFixture(TNthPrimeTest);
end.``````
``````unit uNthPrime;

interface

function NthPrime(N: Integer): Integer;

implementation

uses
SysUtils;

function IsPrime(Int: Integer): Boolean;
var
i: Integer;
begin
Result := True;
for i := 2 to Int - 1 do begin
if Int mod i = 0 then begin
Exit(False);
end;
end;
end;

function NthPrime(N: Integer): Integer;
var
i, PrimeCount: Integer;
begin
if N < 1 then
raise EArgumentOutOfRangeException.Create('there is no zeroth prime');

i := 1; // *The first prime is 2
PrimeCount := 0;
repeat

Inc(i); // *

if IsPrime(i) then
Inc(PrimeCount);

until PrimeCount = N;

Result := i;
end;

end.``````