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

## to Prime Factors in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

## Example

What are the prime factors of 60?

• Our first divisor is 2. 2 goes into 60, leaving 30.
• 2 goes into 30, leaving 15.
• 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
• 3 goes cleanly into 15, leaving 5.
• 3 does not go cleanly into 5. The next possible factor is 4.
• 4 does not go cleanly into 5. The next possible factor is 5.
• 5 does go cleanly into 5.
• We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

• 2 * 2 * 3 * 5
• = 4 * 15
• = 60
• Success!

## 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/all-your-base/AllYourBase.m
``````

You can find the Exercism workspace by running `exercism debug` and looking for the line beginning with Workspace.

## Submitting Incomplete Solutions

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

## Source

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

### PrimeFactorsTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("PrimeFactorsExample.h")
#import "PrimeFactorsExample.h"
#else
#import "PrimeFactors.h"
#endif

@interface PrimeFactorsTest : XCTestCase

@end

@implementation PrimeFactorsTest

- (void)test1 {
XCTAssertEqualObjects(@[], [PrimeFactors factorsForInteger:1]);
}

- (void)test2 {
XCTAssertEqualObjects(@[@2], [PrimeFactors factorsForInteger:2]);
}

- (void)test3 {
XCTAssertEqualObjects(@[@3], [PrimeFactors factorsForInteger:3]);
}

- (void)test4 {
NSArray<NSNumber *> *expected = @[@2, @2];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:4]);
}

- (void)test6 {
NSArray<NSNumber *> *expected = @[@2, @3];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:6]);
}

- (void)test8 {
NSArray<NSNumber *> *expected = @[@2, @2, @2];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:8]);
}

- (void)test9 {
NSArray<NSNumber *> *expected = @[@3, @3];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:9]);
}

- (void)test27 {
NSArray<NSNumber *> *expected = @[@3, @3, @3];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:27]);
}

- (void)test625 {
NSArray<NSNumber *> *expected = @[@5, @5, @5, @5];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:625]);
}

- (void)test901255 {
NSArray<NSNumber *> *expected = @[@5, @17, @23, @461];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:901255]);
}

- (void)test93819012551 {
NSArray<NSNumber *> *expected = @[@11, @9539, @894119];
XCTAssertEqualObjects(expected, [PrimeFactors factorsForInteger:93819012551]);
}

@end``````

### PrimeFactors.h

``````#import <Foundation/Foundation.h>

@interface PrimeFactors : NSObject

+ (NSArray *)factorsForInteger:(NSInteger)number;

@end``````

### PrimeFactors.m

``````#import "PrimeFactors.h"

@implementation PrimeFactors

+ (NSArray *)factorsForInteger:(NSInteger)number {
NSMutableArray *result = [NSMutableArray array];
[self factors:result witnInput:number];
return result;
}

+ (void)factors:(NSMutableArray *)result witnInput:(NSInteger)number {
for (NSInteger i = 2; i <= number; ++i) {
if (number % i == 0) {
[result addObject:@(i)];
[self factors:result witnInput:number / i];
break;
}
}
}

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