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

Published at Jul 13 2018
·
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.

There are two different methods of getting set up to run the tests with Objective-C:

- Create an Xcode project with a test target which will run the tests.
- Use the ruby gem
`objc`

as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

```
exercism submit <path-to-exercism-workspace>/objective-c/nth-prime/NthPrime.m
```

You can find the Exercism workspace by running `exercism debug`

and looking for the line beginning
with Workspace.

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

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
#import <XCTest/XCTest.h>
#if __has_include("BobExample.h")
# import "NthPrimeExample.h"
#else
# import "NthPrime.h"
#endif
NS_ASSUME_NONNULL_BEGIN
@interface NthPrimeTest : XCTestCase
@end
@implementation NthPrimeTest
- (void)testFirstPrime {
int primeNum = 1;
int expected = 2;
int result = [NthPrime primeNum:primeNum];
XCTAssertEqual(expected, result);
}
- (void)testSecondPrime {
int primeNum = 2;
int expected = 3;
int result = [NthPrime primeNum:primeNum];
XCTAssertEqual(expected, result);
}
- (void)testSixthPrime {
int primeNum = 6;
int expected = 13;
int result = [NthPrime primeNum:primeNum];
XCTAssertEqual(expected, result);
}
- (void)testBigPrime {
int primeNum = 10001;
int expected = 104743;
int result = [NthPrime primeNum:primeNum];
XCTAssertEqual(expected, result);
}
- (void)testZeroPrime {
int primeNum = 0;
int expected = 0;
int result = [NthPrime primeNum:primeNum];
XCTAssertEqual(expected, result);
}
@end
NS_ASSUME_NONNULL_END
```

```
//
// nth-prime.h
// nth-prime-tests
//
// Created by Andy Uyeda on 5/16/18.
// Copyright Â© 2018 Andy Uyeda. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface nth_prime : NSObject
+ (int)primeNum:(int) number;
@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