A problem domain is the area of expertise or application that needs to be examined to solve a problem (source: problem domain wiki). It limits the scope of the problem.
A mental model is an explanation of someone's thought process about how something works in the real world (source: wikipedia)
Determine a list of all multiples of a set of factors up to a target value, then filter the list of multiples to the unique values. Finally, compute and return the sum of the unique multiples.
- Create an empty array called
multiplesthat will contain the multiples. - Check whether the list of factors is empty. If there are no factors, set the list to
[3, 5] - For every
factorin thefactorslist:- Set the
current_multipletofactorto keep track of the multiples offactor. - While
current_multiple<target- Append the
current_multipletomultiples. - Add
factortocurrent_multiple.
- Append the
- Set the
- Filter duplicate numbers from
multiples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sum_of_multiples(target, factors) | |
| multiples = [] | |
| factors = [3, 5] if factors.length == 0 | |
| factors.each do |factor| | |
| current_multiple = factor | |
| while current_multiple < target | |
| multiples << current_multiple | |
| current_multiple += factor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumOfMultiples(targetNumber, factors) { | |
| var multiples = []; | |
| if (factors.length === 0) { | |
| factors = [3, 5]; | |
| } | |
| factors.forEach(function(factor) { | |
| var currentMultiple; | |
| for (currentMultiple = factor; currentMultiple < targetNumber; currentMultiple += factor) { | |
| if (multiples.indexOf(currentMultiple) === -1) { |
NewerOlder