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

# NoLongerBreathedIn's solution

## to Difference Of Squares in the Objective-C Track

Published at Oct 23 2018 · 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.

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

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

## Source

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

## Submitting Incomplete Solutions

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

### DifferenceOfSquaresTest.m

``````#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``````

### DifferenceOfSquares.h

``````#import <objc/NSObject.h>
@interface DifferenceOfSquares : NSObject
- (DifferenceOfSquares *) initWithMax : (int) max;
@end``````

### DifferenceOfSquares.m

``````#import "DifferenceOfSquares.h"
@implementation DifferenceOfSquares
- (DifferenceOfSquares *) initWithMax : (int) max {
self = [super init];
long m = max;
_sumOfSquares = (m * (m + 1) * (2 * m + 1)) / 6;
long j = (m * (m + 1)) / 2;
_squareOfSum = j * j;
_differenceOfSquares = _squareOfSum - _sumOfSquares;
return(self);
}
@end``````