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

# jonthejon's solution

## to Sum Of Multiples in the Objective-C Track

Published at Sep 23 2018 · 0 comments
Instructions
Test suite
Solution

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.

## Setup

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.

## Source

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.

### SumOfMultiplesTest.m

#import <XCTest/XCTest.h>

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

@interface SumOfMultiplesTest : XCTestCase

@end

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

@end
#import "SumOfMultiples.h"

@interface SumOfMultiples()
+(NSNumber*)sumOfSet:(NSSet<NSNumber*>*)multiples;
+(NSArray<NSNumber*>*)findMultiplesOfNumber:(NSNumber*)number withLimit:(NSNumber*)limit;
@end

@implementation SumOfMultiples

+(NSNumber *)toLimit:(NSNumber *)limit inMultiples:(NSArray <NSNumber*>*)array {
__block NSMutableSet<NSNumber*>* multiples = [[NSMutableSet alloc] init];
[array enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
}];
return [SumOfMultiples sumOfSet:multiples];
}

+(NSArray<NSNumber *> *)findMultiplesOfNumber:(NSNumber *)number withLimit:(NSNumber *)limit {
if ([number integerValue] <= 0) return @[];
NSMutableArray<NSNumber*>* multiples = [[NSMutableArray alloc] init];
NSInteger current = [number integerValue];
while (current < [limit integerValue]) {
current = current + [number integerValue];
}
return multiples;
}

+(NSNumber *)sumOfSet:(NSSet<NSNumber *> *)multiples {
__block NSInteger count = 0;
[multiples enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, BOOL * _Nonnull stop) {
count = count + [obj integerValue];
}];
return [[NSNumber alloc] initWithInteger:count];
}

@end