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

# 6401's solution

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

Published at Feb 05 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.m

``````//
//  DifferenceOfSquares.m
//  DifferenceOfSquares
//
//  Created by Ð¡ÐµÑ€Ð³ÐµÐ¹ ÐŸÑƒÐ¿Ñ‡Ð¸Ðº on 05/02/2020.
//

#import "DifferenceOfSquares.h"

@implementation DifferenceOfSquares
-(id)initWithMax:(int) i{
self = [super init];
if(self){
_number = i;
}
return self;
}

-(int)squareOfSum{
int sum =0;
for (int j =1;j<=_number;j++){
sum +=j;
}
return sum*sum;
}

-(int)sumOfSquares{
int sum =0;
for (int k=1;k<=_number;k++){
sum +=k*k;
}
return sum;
}

-(int)differenceOfSquares{
int sum =0;
for (int k=1;k<=_number;k++){
sum +=k;
}
sum = sum*sum;
int sum2 =0;
for (int k=1;k<=_number;k++){
sum2 +=k*k;
}
return sum - sum2;
}
@end``````

### DifferenceOfSquares/DifferenceofSquares_Tests/DifferenceOfSquares.h

``````//
//  DifferenceOfSquares.h
//  DifferenceOfSquares
//
//  Created by Ð¡ÐµÑ€Ð³ÐµÐ¹ ÐŸÑƒÐ¿Ñ‡Ð¸Ðº on 05/02/2020.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface DifferenceOfSquares : NSObject
@property int number;
-(id)initWithMax:(int) i;
-(int)squareOfSum;
-(int)sumOfSquares;
-(int)differenceOfSquares;
@end

NS_ASSUME_NONNULL_END``````

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