We're a place where coders share, stay up-to-date and grow their careers.
There's several ways to do it in C++ :)
#include <cstdio> int main () { for (auto i = 1; i < 101; ++i) { if (i % 3 == 0) { std::printf("Fizz"); } if (i % 5 == 0) { std::printf("Buzz"); } std::printf(" %d\n", i); } }
#include <cstdio> struct number { number (int x) : x(x) { } auto operator * () const { return this->x; } bool operator == (int y) { return this->x == y; } bool operator != (int y) { return this->x != y; } number& operator ++ () { ++this->x; return *this; } number operator ++ (int) { return this->x++; } int x; }; struct range { range (int start, int stop) : start(start), stop(stop) { } auto begin () const { return number(this->start); } auto end () const { return this->stop; } private: int start; int stop; }; int main () { for (auto i : range(1, 101)) { if (i % 3 == 0) { std::printf("Fizz"); } if (i % 5 == 0) { std::printf("Buzz"); } std::printf(" %d\n", i); } }
#include <type_traits> #include <utility> #include <cstdio> template <char... Args> struct as_string { const char data[sizeof... (Args)] = { Args... }; }; using fizz = as_string<'f', 'i', 'z', 'z'>; using buzz = as_string<'b', 'u', 'z', 'z'>; using newline = as_string<'\n'>; using null = as_string<'\0'>; struct fizzbuzz: fizz, buzz { }; template <class T> struct type_identity { using type = T; }; template <class T> constexpr auto digits (T x) { auto digits = 0; while (x) { x /= 10; digits++; } return digits; } template <auto Size, auto X, char... Args> struct to_string : to_string<Size - 1, X / 10, '0' + X % 10, Args...> { }; template <auto X, char... Args> struct to_string<1, X, Args...> : type_identity<as_string<'0' + X, Args...>> { }; template <unsigned int X> using int_to_string = typename to_string<digits(X), X>::type; template <auto X, class Fizzable = std::bool_constant<X % 3 == 0>, class Buzzable = std::bool_constant<X % 5 == 0>> struct check; template <auto X> struct check<X, std::false_type, std::false_type> : int_to_string<X> { }; template <auto X> struct check<X, std::true_type, std::false_type> : fizz, int_to_string<X> { }; template <auto X> struct check<X, std::false_type, std::true_type> : buzz, int_to_string<X> { }; template <auto X> struct check<X, std::true_type, std::true_type> : fizzbuzz, int_to_string<X> { }; template <auto X> struct calc : check<X>, newline { }; template <auto... X> struct print : calc<X>..., null { }; template <auto... Is> print<(Is + 1)...> deduce (std::index_sequence<Is...>); template <auto N> using result = decltype(deduce(std::make_index_sequence<N>())); static constexpr result<100> fb{}; int main() { std::puts(reinterpret_cast<const char *>(&fb)); return 0; }
number 3 is the Cthulu of FizzBuzz
That last one scares me. Haha Iām not sure Iād know where to start reading it.
Thanks for jumping into the challenge with some great additions. š
I raise you the compile time calculated fizzbuzz in Nim ;)
proc fizzbuzz(): seq[string] = result = @[] for i in 1..100: result.add( if i mod 15 == 0: "FizzBuzz" elif i mod 5 == 0: "Buzz" elif i mod 3 == 0: "Fizz" else: $i ) const compileTimeValue = fizzbuzz() echo compileTimeValue
Interested in adding this to the collection? š
C++20 isn't available yet, but we're a bit closer to this approach. :)
Take that github.com/EnterpriseQualityCoding...
There's several ways to do it in C++ :)
The Classic
The "GOSH, MOM, IT'S GENERIC"
The "Calculated At Compile Time (stare into madness edition)"
number 3 is the Cthulu of FizzBuzz
That last one scares me. Haha Iām not sure Iād know where to start reading it.
Thanks for jumping into the challenge with some great additions. š
I raise you the compile time calculated fizzbuzz in Nim ;)
Interested in adding this to the collection? š
C++20 isn't available yet, but we're a bit closer to this approach. :)
Take that github.com/EnterpriseQualityCoding...