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

# cedricvaneer's solution

## to Sum Of Multiples in the CFML Track

Published at Jan 21 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.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd` into the exercise folder and run the following:

``````box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher
``````

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

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

### SolutionTest.cfc

``````component extends="SumOfMultiplesTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````

### SumOfMultiplesTest.cfc

``````component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'SumOfMultiples' );
}

function run(){

describe( "My SumOfMultiples class", function(){

it( 'multiples of 3 or 5 up to 1', function(){
expect( SUT.sum( factors=[3, 5], limit='1' ) ).toBe( '0' );
});

it( 'multiples of 3 or 5 up to 4', function(){
expect( SUT.sum( factors=[3, 5], limit='4' ) ).toBe( '3' );
});

it( 'multiples of 3 up to 7', function(){
expect( SUT.sum( factors=[3], limit='7' ) ).toBe( '9' );
});

it( 'multiples of 3 or 5 up to 10', function(){
expect( SUT.sum( factors=[3, 5], limit='10' ) ).toBe( '23' );
});

it( 'multiples of 3 or 5 up to 100', function(){
expect( SUT.sum( factors=[3, 5], limit='100' ) ).toBe( '2318' );
});

it( 'multiples of 3 or 5 up to 1000', function(){
expect( SUT.sum( factors=[3, 5], limit='1000' ) ).toBe( '233168' );
});

it( 'multiples of 7, 13 or 17 up to 20', function(){
expect( SUT.sum( factors=[7, 13, 17], limit='20' ) ).toBe( '51' );
});

it( 'multiples of 4 or 6 up to 15', function(){
expect( SUT.sum( factors=[4, 6], limit='15' ) ).toBe( '30' );
});

it( 'multiples of 5, 6 or 8 up to 150', function(){
expect( SUT.sum( factors=[5, 6, 8], limit='150' ) ).toBe( '4419' );
});

it( 'multiples of 5 or 25 up to 51', function(){
expect( SUT.sum( factors=[5, 25], limit='51' ) ).toBe( '275' );
});

it( 'multiples of 43 or 47 up to 10000', function(){
expect( SUT.sum( factors=[43, 47], limit='10000' ) ).toBe( '2203160' );
});

it( 'multiples of 1 up to 100', function(){
expect( SUT.sum( factors=[1], limit='100' ) ).toBe( '4950' );
});

it( 'multiples of an empty list up to 10000', function(){
expect( SUT.sum( factors=[], limit='10000' ) ).toBe( '0' );
});

});

}

}``````
``````component {

public array function getMulitples(required array factors, required numeric limit) {
var multiples = [];
for (var i = 1; i < arguments.limit; i++) {
for (var factor in arguments.factors) {
if ((i % factor == 0) AND (!arrayFind(multiples, i)) ) {
arrayAppend(multiples, i);
}
}
}
return multiples;
}

public numeric function sum( required array factors, required numeric limit ) {
// Implement me here
return arraySum(getMulitples(arguments.factors, arguments.limit));
}

}``````

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