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

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write code that shows:

- how many grains were on each square, and
- the total number of grains

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

- Optimize for speed.
- Optimize for readability.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

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/grains/Grains.m
```

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

and looking for the line beginning
with Workspace.

JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

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("GrainsExample.h")
# import "GrainsExample.h"
# else
# import "Grains.h"
#endif
NS_ASSUME_NONNULL_BEGIN
@interface GrainsTest : XCTestCase
@end
@implementation GrainsTest
- (void)testNumberOfGrainsOnSquare1 {
long long expected = 1;
long long result = [[[Grains alloc]init]grainsAtSquareNumber:1];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare2 {
long long expected = 2;
long long result = [[[Grains alloc]init]grainsAtSquareNumber:2];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare3 {
unsigned long long expected = 4;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:3];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare4 {
unsigned long long expected = 8;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:4];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare16 {
unsigned long long expected = 32768;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:16];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare32 {
unsigned long long expected = 2147483648;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:32];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare64 {
unsigned long long expected = 9223372036854775808;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:64];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare65 {
unsigned long long expected = -1;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:65];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquareNeg1 {
unsigned long long expected = -1;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:-1];
XCTAssertEqual(expected,result);
}
- (void)testNumberOfGrainsOnSquare0 {
unsigned long long expected = -1;
unsigned long long result = [[[Grains alloc]init]grainsAtSquareNumber:0];
XCTAssertEqual(expected,result);
}
- (void)testTotalNumberOfGrains {
unsigned long long expected = 18446744073709551615;
unsigned long long result = [[[Grains alloc]init]grainsAtBoard];
XCTAssertEqual(expected,result);
}
@end
NS_ASSUME_NONNULL_END
```

```
//
// Grains.h
// Grains
//
// Created by Jaishree Dhage on 12/12/17.
// Copyright Â© 2017 Jaishree Dhage. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Grains : NSObject
-(unsigned long long)grainsAtSquareNumber:(NSUInteger) number;
-(unsigned long long)grainsAtBoard;
@end
```

```
//
// Grains.m
// Grains
//
// Created by Jaishree Dhage on 12/12/17.
// Copyright Â© 2017 Jaishree Dhage. All rights reserved.
//
#import "Grains.h"
@implementation Grains
-(unsigned long long)grainsAtSquareNumber:(NSUInteger)number{
if(number <= 0 || number>64)
return -1;
unsigned long long grains = pow(2,number-1);
return grains;
}
-(unsigned long long)grainsAtBoard{
unsigned long long totalGrains = pow(2,64)-1; //actual value overflows
return 18446744073709551615;
}
@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