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

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

Published at Dec 18 2020 · 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/DifferenceOfSquares Tests/DifferenceOfSquares.h

``````//
//  DifferenceOfSquares.h
//  DifferenceOfSquares
//
//  Created by Nadiar Syaripul on 18/12/20.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DifferenceOfSquares : NSObject

@property int nCount;

- (id) initWithMax:(int)maxValue;
- (int) squareOfSum;
- (int) sumOfSquares;
- (int) differenceOfSquares;
@end

NS_ASSUME_NONNULL_END``````

### DifferenceOfSquares/DifferenceOfSquares Tests/DifferenceOfSquares.m

``````//
//  DifferenceOfSquares.m
//  DifferenceOfSquares
//
//  Created by Nadiar Syaripul on 18/12/20.
//

#import "DifferenceOfSquares.h"
#include <math.h>

@implementation DifferenceOfSquares

- (id) initWithMax:(int)nCount {
self = [super init];
if (self) {
[self setNCount:nCount];
}
return self;
}

- (int) squareOfSum {
int result = 0;
int count = [self nCount] + 1;
for (int i=1; i<count; i++) {
result += i;
}
return pow(result, 2);
}

- (int) sumOfSquares {
int result = 0;
int count = [self nCount] + 1;
for (int i=1; i<count; i++) {
result += pow(i, 2);
}
return result;
}

- (int) differenceOfSquares {
return [self squareOfSum] - [self sumOfSquares];
}

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