This program calculates the sum of all integers in the range \$[1, 1000)\$ which are multiples of either 3 or 5 or both.
Inspired by x86-64 Assembly - Sum of multiples of 3 or 5 the other day, and also drawing from some of what I learned through Calculate the centroid of a collection of complex numbers I decided to try to tackle this using C++20 ranges. By heavy use of constexpr
, I had hoped to find a solution that would calculate everything at compile time, and indeed this does as you can see if you try it online.
This version is inspired by this talk C++20 Ranges in Practice - Tristan Brindle - CppCon 2020.
I'm interested in general improvements.
euler1.cpp
#include <iostream>
#include <concepts>
#include <ranges>
#include <iterator>
#include <functional>
#include <numeric>
template <std::ranges::input_range R,
typename Init = std::ranges::range_value_t<R>>
constexpr Init accumulate(R&& rng, Init init = Init{})
{
return std::reduce(std::ranges::begin(rng), std::ranges::end(rng),
std::move(init));
}
int main() {
constexpr auto div3_or_5 = [](int i){ return i % 3 == 0 || i % 5 == 0; };
std::cout << accumulate(std::ranges::iota_view{1, 1000}
| std::views::filter(div3_or_5)
| std::views::common)
<< '\n';
}
:_)
\$\endgroup\$