An even number is an integer divisible by two without a remainder. In C++, test parity with i % 2: a zero result marks an even number. section 9.1.4
Use the parity test to classify values:
- Return '*' when i % 2 is zero, identifying an even number. section 9.1.4
- Return '.' when i % 2 is nonzero, identifying an odd number. section 9.1.4
Triangle-number parity
The first triangle numbers are 1, 3, 6, and 10. Their parity markers are '.', '.', '*', and '*'. Applying the transformation to the first 20 values produces a repeating marker sequence. section 9.1.4
Listing 9.5 Checking whether the numbers are odd or even
#include <algorithm>
#include <iostream>
#include <iterator>
void demo_further_properties()
{
const int count = 20;
const auto triangle_numbers = make_triangle_numbers(count);
std::vector<char> odd_or_even; #1
std::ranges::transform(triangle_numbers,
std::back_inserter(odd_or_even),
[](int i) { return i % 2 ? '.' : '*'; }); #2
std::ranges::copy(odd_or_even,
std::ostream_iterator<char>(std::cout, " ")); #3
std::cout << '\n';
}
Inspect a complete C++ function that transforms triangle numbers into odd/even markers and copies them to standard output. section 9.1.4
