Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of lockheedbird

lockheedbird's solution

to Sum Of Multiples in the Objective-C Track

Published at Jan 13 2019 · 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.


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/sum-of-multiples/SumOfMultiples.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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/XCTest.h>

#if __has_include("SumOfMultiplesExample.h")
# import "SumOfMultiplesExample.h"
# else
# import "SumOfMultiples.h"

@interface SumOfMultiplesTest : XCTestCase


@implementation SumOfMultiplesTest

- (void)testSumTo1 {
    NSNumber *sum = [SumOfMultiples toLimit:@1 inMultiples:@[@3, @5]];
    XCTAssertEqualObjects(@0, sum);

- (void)testSumTo3 {
    NSNumber *sum = [SumOfMultiples toLimit:@4 inMultiples:@[@3, @5]];
    XCTAssertEqualObjects(@3, sum);

- (void)testSumTo10 {
    NSNumber *sum = [SumOfMultiples toLimit:@10 inMultiples:@[@3, @5]];
    XCTAssertEqualObjects(@23, sum);

- (void)testSumTo100 {
    NSNumber *sum = [SumOfMultiples toLimit:@100 inMultiples:@[@3, @5]];
    XCTAssertEqualObjects(@2318, sum);

- (void)testSumTo1000 {
    NSNumber *sum = [SumOfMultiples toLimit:@1000 inMultiples:@[@3, @5]];
    XCTAssertEqualObjects(@233168, sum);

- (void)testConfigurable_7_13_17_to_20 {
    NSNumber *sum = [SumOfMultiples toLimit:@20 inMultiples:@[@7, @13, @17]];
    XCTAssertEqualObjects(@51, sum);

- (void)testConfigurable_4_6_to_15 {
    NSNumber *sum = [SumOfMultiples toLimit:@15 inMultiples:@[@4, @6]];
    XCTAssertEqualObjects(@30, sum);

- (void)testConfigurable_5_6_8_to_150 {
    NSNumber *sum = [SumOfMultiples toLimit:@150 inMultiples:@[@5, @6, @8]];
    XCTAssertEqualObjects(@4419, sum);

- (void)testConfigurable_43_47_to_10000 {
    NSNumber *sum = [SumOfMultiples toLimit:@10000 inMultiples:@[@43, @47]];
    XCTAssertEqualObjects(@2203160, sum);

- (void)testConfigurable_0_to_10 {
    NSNumber *sum = [SumOfMultiples toLimit:@10 inMultiples:@[@0]];
    XCTAssertEqualObjects(@0, sum);

- (void)testConfigurable_0_1_to_10 {
    NSNumber *sum = [SumOfMultiples toLimit:@10 inMultiples:@[@0, @1]];
    XCTAssertEqualObjects(@45, sum);

#import "SumOfMultiples.h"

@implementation SumOfMultiples

+ (NSNumber *)toLimit:(NSNumber*)limit inMultiples:(NSArray *)multiples
    NSNumber *sum = @0;
    NSMutableSet *setOfNumbers = [NSMutableSet setWithObject:@0];
    for (int i = 0; i < limit.intValue; i++)
        for (NSNumber *n in multiples)
            if (n.integerValue > 0 && i % n.integerValue == 0)
                [setOfNumbers addObject:@(i)];
    for (NSNumber *n in setOfNumbers)
        sum = @(sum.integerValue + n.integerValue);
    return sum;


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?