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
multiples
that 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
factor
in thefactors
list:- Set the
current_multiple
tofactor
to keep track of the multiples offactor
. - While
current_multiple
<target
- Append the
current_multiple
tomultiples
. - Add
factor
tocurrent_multiple
.
- Append the
- Set the
- Filter duplicate numbers from
multiples
. - Compute and return the sum of the numbers in
multiples
.
Incrementally build a list of numbers that are multiples of a set of one or more factors. Add a multiple to the list only if it is not yet on the list. Finally, compute and return the sum of the numbers on the list.
- Create an empty array called
multiples
that will contain the list of multiples - Check whether the list of factors is empty. If there are no factors, set the list to
[3, 5]
- For every
factor
in thefactors
list:- Set the
current_multiple
tofactor
to keep track of the multiples offactor
. - While
current_multiple
<target
- Is the
current_multiple
inmultiples
already?- Yes - do nothing
- No - Append the
current_multiple
tomultiples
.
- Add
factor
tocurrent_multiple
.
- Is the
- Set the
- Compute and return the sum of the numbers in
multiples
.