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

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

Published at May 12 2019 · 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.m
//  sum-of-multiples-core
//
//  Created by Pietro Caselani on 12/05/19.
//  Copyright © 2019 PC. All rights reserved.
//

#import "SumOfMultiples.h"

@implementation NSArray (Join)

- (NSString *)join {
NSMutableString *string = [[NSMutableString alloc] init];

for (NSObject *o in self) {
[string appendString:[o description]];
[string appendString:@", "];
}

return [NSString stringWithString:string];
}

@end

@implementation SumOfMultiples

+ (NSNumber *)toLimit:(NSNumber *)limit inMultiples:(NSArray<NSNumber*>*)multiples {
int interactions = 0;

if ([limit isEqualToNumber:@1] || [limit isEqualToNumber:@0]) {
NSLog(@">>> interactions = %d | limit = %@ multiples = %@", interactions, limit, [multiples join]);
return @0;
}

NSMutableArray *alreadyMultiplied = [[NSMutableArray alloc] init];

float total = 0;
NSNumber *min = limit;

for (NSNumber *multiple in multiples) {
interactions += 1;
if ([multiple isLessThan:min]) {
min = multiple;
}
}

for (NSInteger i = [min integerValue]; i < [limit integerValue]; i++) {
for (NSNumber *multiple in multiples) {
interactions += 1;
if ([multiple isNotEqualTo:@0] && i % [multiple integerValue] == 0 && ![alreadyMultiplied containsObject:@(i)]) {
[alreadyMultiplied addObject:@(i)];
total += i;
}

}
}

NSLog(@">>> interactions = %d | limit = %@ multiples = %@", interactions, limit, [multiples join]);

return @(total);
}

//+ (NSNumber *)toLimit:(NSNumber *)limit inMultiples:(NSArray<NSNumber*>*)multiples {
//    NSMutableArray *alreadyMultiplied = [[NSMutableArray alloc] init];
//
//    float total = 0;
//    int interactions = 0;
//
//    for (NSInteger i = 1; i < [limit integerValue]; i++) {
//        for (NSNumber *multiple in multiples) {
//            interactions += 1;
//            if ([multiple isNotEqualTo:@0] && i % [multiple integerValue] == 0 && ![alreadyMultiplied containsObject:@(i)]) {
//                [alreadyMultiplied addObject:@(i)];
//                total += i;
//            }
//        }
//    }
//
//    NSLog(@">>> interactions = %d | limit = %@ multiples = %@", interactions, limit, [multiples join]);
//
//    return @(total);
//}

@end``````

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