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.
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.
Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the
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.
A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7
It's possible to submit an incomplete solution so you may request help from a mentor.
unit uNthPrimeTests; interface uses DUnitX.TestFramework; const CanonicalVersion = '188.8.131.52'; 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.
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.