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

Published at Apr 14 2019
·
0 comments

Instructions

Test suite

Solution

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)Â² = 55Â² = 3025.

The sum of the squares of the first ten natural numbers is 1Â² + 2Â² + ... + 10Â² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.

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/difference-of-squares/DifferenceOfSquares.m
```

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

and looking for the line beginning
with Workspace.

Problem 6 at Project Euler http://projecteuler.net/problem=6

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("DifferenceOfSquaresExample.h")
# import "DifferenceOfSquaresExample.h"
# else
# import "DifferenceOfSquares.h"
#endif
@interface DifferenceOfSquaresTest : XCTestCase
@end
@implementation DifferenceOfSquaresTest
- (void)testSquareOfSumTo5 {
XCTAssertEqual(225, [[[DifferenceOfSquares alloc] initWithMax:5] squareOfSum]);
}
- (void)testSumOfSquaresTo5 {
XCTAssertEqual(55, [[[DifferenceOfSquares alloc] initWithMax:5] sumOfSquares]);
}
- (void)testDifferenceOfSquaresOfSumTo5 {
XCTAssertEqual(170, [[[DifferenceOfSquares alloc] initWithMax:5] differenceOfSquares]);
}
- (void)testSquareOfSumTo10 {
XCTAssertEqual(3025, [[[DifferenceOfSquares alloc] initWithMax:10] squareOfSum]);
}
- (void)testSumOfSquaresTo10 {
XCTAssertEqual(385, [[[DifferenceOfSquares alloc] initWithMax:10] sumOfSquares]);
}
- (void)testDifferenceOfSquaresOfSumTo10 {
XCTAssertEqual(2640, [[[DifferenceOfSquares alloc] initWithMax:10] differenceOfSquares]);
}
- (void)testSquareOfSumTo100 {
XCTAssertEqual(25502500, [[[DifferenceOfSquares alloc] initWithMax:100] squareOfSum]);
}
- (void)testSumOfSquaresTo100 {
XCTAssertEqual(338350, [[[DifferenceOfSquares alloc] initWithMax:100] sumOfSquares]);
}
- (void)testDifferenceOfSquaresOfSumTo100 {
XCTAssertEqual(25164150, [[[DifferenceOfSquares alloc] initWithMax:100] differenceOfSquares]);
}
@end
```

```
#import "DifferenceOfSquares.h"
@implementation DifferenceOfSquares
- (id) initWithMax: (int) max {
self = [super init];
if (self) {
_max = max;
}
return self;
}
+ (int) square: (int) n {
return n * n;
}
- (int) squareOfSum {
return [DifferenceOfSquares square: (_max * (_max + 1) / 2)];
}
- (int) sumOfSquares {
return (_max * (_max + 1) * ((2 * _max) + 1)) / 6;
}
- (int) differenceOfSquares {
return [self squareOfSum] - [self sumOfSquares];
}
@end
```

```
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DifferenceOfSquares : NSObject
@property int max;
- (id) initWithMax: (int) max;
- (int) squareOfSum;
- (int) sumOfSquares;
- (int) differenceOfSquares;
+ (int) square: (int) n;
@end
NS_ASSUME_NONNULL_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