🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # nadiar429's solution

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

Published at Dec 19 2020 · 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``````

### SumOfMultiples/SumOfMultiples Tests/SumOfMultiples.h

``````//
//  SumOfMultiples.h
//  SumOfMultiples
//
//  Created by Nadiar Syaripul on 19/12/20.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface SumOfMultiples : NSObject

+ (NSNumber *) toLimit:(NSNumber *)start inMultiples:(NSArray<NSNumber *> *)factorOf;

@end

NS_ASSUME_NONNULL_END``````

### SumOfMultiples/SumOfMultiples Tests/SumOfMultiples.m

``````//
//  SumOfMultiples.m
//  SumOfMultiples
//
//  Created by Nadiar Syaripul on 19/12/20.
//

#import "SumOfMultiples.h"
#import "math.h"

@implementation SumOfMultiples

+ (NSNumber *) toLimit:(NSNumber *)end inMultiples:(NSArray<NSNumber *> *)factorOf {
NSMutableSet *set = [[NSMutableSet alloc] init];
for (NSNumber *value in factorOf) {
NSArray *coll = [SumOfMultiples sequence:value until:end];
[set addObjectsFromArray:coll];
}

int sum = 0;
for (NSNumber *value in set) {
sum += [value intValue];
}

return [NSNumber numberWithInt:sum];
}

+ (NSArray<NSNumber *> *) sequence:(NSNumber *)nom until:(NSNumber *)limit {
if ([nom intValue] == 0) {
return @[@0];
}

NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=[nom intValue]; i<[limit intValue]; i += [nom intValue]) {
[arr addObject:[NSNumber numberWithInt:i]];
}

return arr;
}

@end``````

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