🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # dgeiger's solution

## to Nth Prime in the Delphi Pascal Track

Published at Sep 06 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.

### Loading Exercises into Delphi

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

uses
System.Math, System.SysUtils;

type
EArgumentOutOfRangeException = class(Exception);

function NthPrime(Number: Integer): Integer;

implementation

function Sieve(Candidate: Integer): Boolean;
var
Factor: Integer;
begin
// Since primes are rare, default to non-prime
Result := False;

// A prime number must be a positive integer greater than one. If the
// candidate number doesn't meet this criterion, leave early.
if Candidate < 2 then
exit;

// If Candidate is two, we can short cut here, since two is the first prime,
// and is also the only even prime.
if Candidate = 2 then
begin
Result := True;

exit;
end;

// Three is the first odd prime, so we start there
Factor := 3;

// We only need to check 3..Sqrt(Candidate). As part of this we need to
// for Candidate to be a real number for Sqrt().
while Factor <= Trunc(Sqrt(Candidate * 1.0)) do
begin
// Is this factor a prime factor?
if Candidate mod Factor = 0 then
// This factor is a factor for Candidate, so Candidate isn't prime.
Exit;

// Try the next possible factor
Inc(Factor, 2);
end;

// If we got this far, Candidate is prime
Result := True;
end;

function NthPrime(Number: Integer): Integer;
var
Count: Integer;
Current: Integer;
begin
// The count of primes must be a positive integer.
if Number < 1 then
raise EArgumentOutOfRangeException.Create('there is no zeroth prime');

// We handle the first prime differently than all others. The first prime is
// two, which is also the only even prime.
if Number = 1 then
begin
Result := 2;

Exit;
end;

// We already know the first prime, so we set the count to one. This is done
// to simplify the code in the loop.
Count := 1;

// Set the current candidate to start at one
Current := 1;

// We need to keep looping until we find the correct prime
while Count < Number do
begin
// Since we know all remaining primes are odd, we move to the
// next odd candidate
Inc(Current, 2);

// Is the current candidate prime?
if Sieve(Current) then
// Yes - increment the count of primes
Inc(Count);
end;

// We found the correct prime, so return it
Result := Current;
end;

end.``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

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?