Skip to content

Instantly share code, notes, and snippets.

@ecatmur
Created October 29, 2020 17:20
Show Gist options
  • Save ecatmur/261d2f684188cdcb4e3a9161b4077c64 to your computer and use it in GitHub Desktop.
Save ecatmur/261d2f684188cdcb4e3a9161b4077c64 to your computer and use it in GitHub Desktop.
#include <boost/preprocessor.hpp>
#include <iostream>
template<int r> void print(int i) {
if constexpr (r)
if constexpr (r % 5)
if constexpr (r % 3)
std::cout << i << std::endl;
else
std::cout << "fizz" << std::endl;
else
std::cout << "buzz" << std::endl;
else
std::cout << "fizzbuzz" << std::endl;
}
void fizzbuzz(int min, int max) {
int i = min;
switch (min % 15) {
#define header(z, n, data) case n : i += 15 - n; break;
BOOST_PP_REPEAT_FROM_TO(1, 15, header, _)
}
switch (min % 15) {
#define header2(z, n, data) case n : print<n>(min + n - 1);
BOOST_PP_REPEAT_FROM_TO(1, 15, header2, _)
}
for (; i + 15 <= max; i += 15) {
#define body(z, n, data) print<n>(i + n);
BOOST_PP_REPEAT_FROM_TO(0, 15, body, _)
}
switch (max - i) {
#define trailer2(z, n, data) print<n>(i + n);
#define trailer(z, r, data) case r : BOOST_PP_CAT(BOOST_PP_REPEAT_FROM_TO_, z)(0, BOOST_PP_INC(r), trailer2, _); break;
BOOST_PP_REPEAT_FROM_TO(0, 15, trailer, _)
}
}
int main() {
fizzbuzz(1, 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment