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

## to Nth Prime in the Objective-C Track

Published at Jul 22 2019 · 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.

## Setup

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

### Submitting Exercises

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.

## 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 can see how others have completed the exercise.

### NthPrimeTest.m

``````#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-tests/NthPrime.h

``````//
//  NthPrime.h
//  nth-prime-tests
//
//  Created by Ingemar Pertl on 22.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NthPrime : NSObject

+ (int)primeNum:(int)number;

@end

NS_ASSUME_NONNULL_END``````

### nth-prime-tests/NthPrime.m

``````//
//  NthPrime.m
//  nth-prime-tests
//
//  Created by Ingemar Pertl on 22.07.19.
//  Copyright © 2019 Ingemar Pertl. All rights reserved.
//

#import "NthPrime.h"

@interface NthPrime ()

+ (BOOL)isPrime:(int)number;

@end

@implementation NthPrime

+ (int)primeNum:(int)number {
NSMutableArray<NSNumber *> *primes = [NSMutableArray new];
int primeSuggestion = 2;
while (primes.count < number) {
if([NthPrime isPrime:primeSuggestion])
[primes addObject:[NSNumber numberWithInt:primeSuggestion]];

primeSuggestion++;
}

return [[primes lastObject] intValue];
}

+ (BOOL)isPrime:(int)number {
int dividersCount = 2; // 1 and the number itself

for (int i = 2; i < number; i++) {
if(i != number && number % i == 0)
dividersCount++;
}

return dividersCount == 2;
}

@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?