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

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
    }
}

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?