pub fn sum_of_multiples_from_factors(limit: u32, factors: &[u32]) -> u32 {
factors
.iter()
.filter(|&&factor| factor != 0)
.flat_map(|&factor| (factor..limit).step_by(factor as usize))
.collect::<HashSet<_>>()
.iter()
.sum()
}
This approach implements the exact steps outlined in the exercise description:
- For each non-zero factor, find all multiples of that factor that are less than the
limit
- Collect all multiples in a
HashSet
- Calculate the sum of all unique multiples
In order to compute the list of multiples for a factor, we create a Range
from the factor (inclusive) to the limit
(exclusive), then use step_by
with the same factor.
To combine the multiples of all factors, we iterate the list of factors and use flat_map
on each factor's multiples.
flat_map
is a combination of map
and flatten
; it maps each factor into its multiples, then flattens them all in a single sequence.
Since we need to have unique multiples to compute the proper sum, we collect
the multiples into a HashSet
, which only keeps one of each of its entries, thus removing duplicates.
collect
is a powerful function that can collect the data in a sequence and store it in any kind of collection - however, because of this, the compiler in this case is not able to infer the type of collection you want as the output.
To solve this problem, we specify the type HashSet<_>
explicitly.
Finally, calculating the sum of the remaining unique multiples in the set is easy: we can simply get an Iterator and call sum
.