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

Eimio's solution

to Sum Of Multiples in the Swift Track

Published at Apr 22 2021 · 0 comments
Test suite

Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18.

The sum of these multiples is 78.


Go through the project setup instructions for Xcode using Swift:


Notably from the source directory:

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


A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import XCTest
@testable import SumOfMultiplesTests



import XCTest
@testable import SumOfMultiples

class SumOfMultiplesTests: XCTestCase {
    func testSumTo1() {
        XCTAssertEqual(0, SumOfMultiples.toLimit(1, inMultiples: [3, 5]))

    func testSumTo3() {
        XCTAssertEqual(3, SumOfMultiples.toLimit(4, inMultiples: [3, 5]))

    func testSumTo10() {
        XCTAssertEqual(23, SumOfMultiples.toLimit(10, inMultiples: [3, 5]))

    func testSumTo100() {
        XCTAssertEqual(2318, SumOfMultiples.toLimit(100, inMultiples: [3, 5]))

    func testSumTo1000() {
        XCTAssertEqual(233168, SumOfMultiples.toLimit(1000, inMultiples: [3, 5]))

    func testConfigurable_7_13_17_to_20() {
        XCTAssertEqual(51, SumOfMultiples.toLimit(20, inMultiples: [7, 13, 17]))

    func testConfigurable_4_6_to_15() {
        XCTAssertEqual(30, SumOfMultiples.toLimit(15, inMultiples: [4, 6]))

    func testConfigurable_5_6_8_to_150() {
        XCTAssertEqual(4419, SumOfMultiples.toLimit(150, inMultiples: [5, 6, 8]))

    func testConfigurable_43_47_to_10000() {
        XCTAssertEqual(2203160, SumOfMultiples.toLimit(10000, inMultiples: [43, 47]))

    func testConfigurable_0_to_10() {
        XCTAssertEqual(0, SumOfMultiples.toLimit(10, inMultiples: [0]))

    func testConfigurable_0_1_to_10() {
        XCTAssertEqual(45, SumOfMultiples.toLimit(10, inMultiples: [0, 1]))

    func testConfigurable_0_27_to_0() {
        XCTAssertEqual(0, SumOfMultiples.toLimit(0, inMultiples: [0, 27]))

    static var allTests: [(String, (SumOfMultiplesTests) -> () throws -> Void)] {
        return [
            ("testSumTo1", testSumTo1),
            ("testSumTo3", testSumTo3),
            ("testSumTo10", testSumTo10),
            ("testSumTo100", testSumTo100),
            ("testSumTo1000", testSumTo1000),
            ("testConfigurable_7_13_17_to_20", testConfigurable_7_13_17_to_20),
            ("testConfigurable_4_6_to_15", testConfigurable_4_6_to_15),
            ("testConfigurable_5_6_8_to_150", testConfigurable_5_6_8_to_150),
            ("testConfigurable_43_47_to_10000", testConfigurable_43_47_to_10000),
            ("testConfigurable_0_to_10", testConfigurable_0_to_10),
            ("testConfigurable_0_1_to_10", testConfigurable_0_1_to_10),
            ("testConfigurable_0_27_to_0", testConfigurable_0_27_to_0)
import Foundation

class SumOfMultiples {
    static func toLimit(_ limit: Int, inMultiples: [Int]) -> Int {
        return Array(0..<limit).filter { number in
            inMultiples.map { mod in
                number.isMultiple(of: mod)

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?