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

# n0mn0m's solution

## to Difference Of Squares in the Swift Track

Published at Nov 24 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

Go through the project setup instructions for Xcode using Swift:

http://exercism.io/languages/swift
http://exercism.io/languages/swift/tests

Notably from the source directory:

`swift test` runs tests
`swift package generate-xcodeproj` creates an Xcode project

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

### DifferenceOfSquaresTests.swift

``````import XCTest
@testable import DifferenceOfSquares

class DifferenceOfSquaresTests: XCTestCase {

func testSquareOfSumTo5() {
XCTAssertEqual(225, Squares(5).squareOfSum)
}

func testSumOfSquaresTo5() {
XCTAssertEqual(55, Squares(5).sumOfSquares)
}

func testDifferenceOfSquaresOfSumTo5() {
XCTAssertEqual(170, Squares(5).differenceOfSquares)
}

func testSquareOfSumTo10() {
XCTAssertEqual(3025, Squares(10).squareOfSum)
}

func testSumOfSquaresTo10() {
XCTAssertEqual(385, Squares(10).sumOfSquares)
}

func testDifferenceOfSquaresOfSumTo10() {
XCTAssertEqual(2640, Squares(10).differenceOfSquares)
}

func testSquareOfSumTo100() {
XCTAssertEqual(25_502_500, Squares(100).squareOfSum)
}

func testSumOfSquaresTo100() {
XCTAssertEqual(338_350, Squares(100).sumOfSquares)
}

func testDifferenceOfSquaresOfSumTo100() {
XCTAssertEqual(25_164_150, Squares(100).differenceOfSquares)
}

static var allTests: [(String, (DifferenceOfSquaresTests) -> () throws -> Void)] {
return [
("testSquareOfSumTo5", testSquareOfSumTo5),
("testSumOfSquaresTo5", testSumOfSquaresTo5),
("testDifferenceOfSquaresOfSumTo5", testDifferenceOfSquaresOfSumTo5),
("testSquareOfSumTo10", testSquareOfSumTo10),
("testSumOfSquaresTo10", testSumOfSquaresTo10),
("testDifferenceOfSquaresOfSumTo10", testDifferenceOfSquaresOfSumTo10),
("testSquareOfSumTo100", testSquareOfSumTo100),
("testSumOfSquaresTo100", testSumOfSquaresTo100),
("testDifferenceOfSquaresOfSumTo100", testDifferenceOfSquaresOfSumTo100),
]
}
}``````

### LinuxMain.swift

``````import XCTest
@testable import DifferenceOfSquaresTests

XCTMain([
testCase(DifferenceOfSquaresTests.allTests),
])``````
``````//Solution goes in Sources

func square(_ n: Int) -> Int {
n * n
}

public class Squares {
let squareOfSum: Int
let sumOfSquares: Int
let differenceOfSquares: Int

init(_ n: Int) {
let numbers = (1...n).lazy
sumOfSquares = numbers.map(square).reduce(0, +)
squareOfSum = square(numbers.reduce(0, +))
differenceOfSquares = squareOfSum - sumOfSquares
}
}``````