ðŸŽ‰ 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

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. The conjecture states that no matter which number you start with, you will always reach 1 eventually.

Given a number n, return the number of steps required to reach 1.

Starting with n = 12, the steps would be as follows:

- 12
- 6
- 3
- 10
- 5
- 16
- 8
- 4
- 2
- 1

Resulting in 9 steps. So for input n = 12, the return value would be 9.

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/collatz-conjecture/CollatzConjecture.m
```

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

and looking for the line beginning
with Workspace.

An unsolved problem in mathematics named after mathematician Lothar Collatz https://en.wikipedia.org/wiki/3x_%2B_1_problem

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("CollatzConjectureExample.h")
# import "CollatzConjectureExample.h"
# else
# import "CollatzConjecture.h"
#endif
@interface CollatzConjectureTest : XCTestCase
@end
@implementation CollatzConjectureTest
- (void)testZeroStepsForOne {
XCTAssertEqual(0, [CollatzConjecture stepsForNumber:1]);
}
- (void)testDivideIfEven {
XCTAssertEqual(4, [CollatzConjecture stepsForNumber:16]);
}
- (void)testEvenAndOddSteps {
XCTAssertEqual(9, [CollatzConjecture stepsForNumber:12]);
}
- (void)testLargeNumberOfEvenAndOddSteps {
XCTAssertEqual(152, [CollatzConjecture stepsForNumber:1000000]);
}
- (void)testZeroIsAnError {
XCTAssertEqual([CollatzConjecture stepsForNumber:0], NSNotFound);
}
- (void)testNegativeValueIsAnError {
XCTAssertEqual([CollatzConjecture stepsForNumber:-15], NSNotFound);
}
@end
```

```
#import <Foundation/Foundation.h>
@interface CollatzConjecture : NSObject
+ (NSInteger)stepsForNumber:(NSInteger)number;
@end
```

```
#import "CollatzConjecture.h"
@implementation CollatzConjecture
+ (NSInteger)stepsForNumber:(NSInteger)number {
if (number <= 0) {
return NSNotFound;
}
NSInteger steps = 0;
while (number != 1) {
if (number % 2 == 0) {
number /= 2;
} else {
number = number * 3 + 1;
}
steps++;
}
return steps;
}
@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