🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of atfelix

atfelix's solution

to Difference Of Squares in the Objective-C Track

Published at Aug 21 2018 · 0 comments
Test suite

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

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.


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.


#import <XCTest/XCTest.h>

#if __has_include("DifferenceOfSquaresExample.h")
# import "DifferenceOfSquaresExample.h"
# else
# import "DifferenceOfSquares.h"

@interface DifferenceOfSquaresTest : XCTestCase


@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]);


DifferenceOfSquares/DifferenceOfSquares Tests/DifferenceOfSquares.h

@import Foundation;

@interface DifferenceOfSquares : NSObject

@property (nonatomic) NSInteger squareOfSum;
@property (nonatomic) NSInteger sumOfSquares;
@property (nonatomic) NSInteger differenceOfSquares;



DifferenceOfSquares/DifferenceOfSquares Tests/DifferenceOfSquares.m

#import "DifferenceOfSquares.h"

NSInteger sumOfSquares(NSInteger n);
NSInteger squaresOfSums(NSInteger n);

NSInteger squaresOfSums(NSInteger n) {
    return (n * (n + 1) / 2) * (n * (n + 1) / 2);

NSInteger sumOfSquares(NSInteger n) {
    return (2 * n + 1) * n * (n + 1) / 6;

@implementation DifferenceOfSquares

-(instancetype)initWithMax:(NSInteger)number {
    self = [super init];
    if (self) {
        _sumOfSquares = sumOfSquares(number);
        _squareOfSum = squaresOfSums(number);
        _differenceOfSquares = _squareOfSum - _sumOfSquares;
    return self;


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?