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

Published at Oct 25 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.

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

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

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"
#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 <objc/NSObject.h>
@interface SumOfMultiples : NSObject
+ (NSNumber *) toLimit: (NSNumber *) lim
inMultiples: (NSArray<NSNumber *> *)mul;
@end
```

```
#import "SumOfMultiples.h"
#include <stdlib.h>
@implementation SumOfMultiples
+ (NSNumber *) toLimit: (NSNumber *) lim
inMultiples: (NSArray<NSNumber *> *) mul {
NSInteger l = lim.integerValue;
NSInteger s = 0;
NSUInteger c = mul.count;
NSInteger *a = malloc(sizeof(NSInteger) * c);
for(NSUInteger i = 0; i < c; i++)
a[i] = mul[i].integerValue;
while(--l)
for(NSInteger j = 0; j < c; j++)
if(a[j] && !(l % a[j])) {
s += l;
break;
}
free(a);
return [NSNumber numberWithInteger:s];
}
@end
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments