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

# webmandman's solution

## to Sum Of Multiples in the CFML Track

Published at Dec 06 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.

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
``````

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

});

}

}``````
``````/**
* Your implmentation of the SumOfMultiples exercise
*/
component {

/**
* @returns
*/
function sum( array factors, limit ) {
var uniques = [];
var multiple = 0;
// add the multiples of the factors less than the limit
// and add them to the unique list.
// loop through each item in factors array
for(var i = 1; i <= factors.len();i++){
// lets get the multiples of each factor
for(var j = 1; j <= limit; j++){
multiple = j*factors[i];
if(not uniques.contains(multiple) and multiple lt limit){
uniques.append(multiple);
}
}
}
return uniques.sum();
}

}``````