This little “poor-developer filter” was discussed and solved many times.

Despite the number of discussions it spur, and the many suggested ways to solve the problem, the question regarding which solution would be the most efficient, remained unclear. Maybe because its original goal was to screen “bad” developers rather than actually solve the problem efficiently or demonstrate creativity. It was also said that during an interview, solving the problem in more efficient less readable manner would disqualify the developer.

The FizzBuzz problem seems so naive and simple to solve, that it becomes an even more intriguing task to optimize it. So I’ve looked around the various suggestions (more than 80 languages!), and isolated the key ways to solve the problem. Then I added a simple timing and below are the results. Which FizzBuzz solution is the most efficient?

It seems that there are 4 main patterns to solve the FizzBuzz problem:

  1. Using modulo and int equality test.
  2. Using modulo and string equality test.
  3. Count 3 and 5 down or up as i progress (testing counts of 3 and 5).
  4. Using some bitwise trickery.

I’m using C# in the implementation example, yet the above methods apply to all languages (possibly languages will deffer being better than others comparing ints vs. comparing strings).

Note that I’m using the + sign to concatenate the strings in my examples, yet a more efficient way in C# will be to use StringBuilder. @Thanks Matt!

As shown below, the most effective methods were the ones with: 1. the least int equality tests, and 2. those using less modulo.

The most efficient? we almost have a tie between the following 3 marked in yellow.

Comparing int values (in loop), using 4 (in loop) modulo operations and chained if..else:

Selec All Code:

Comparing int values, using 2 (in loop) modulo operations and 4 (in loop) int equality tests:

Selec All Code:

Comparing strings, using 2 (in loop) modulo operations:

Selec All Code:

Counting up, using 2 single (out of loop) modulo operations:

Selec All Code:

Counting down, using no modulo operations, chained if..else:

Selec All Code:

All Bitwise operations (using predefined 0-15 numbers mask):

Selec All Code:

@Download the full C# code


Can you find a more efficient way?

For more exploration, visit this discussion: .




  • Matt Johnson

    Well, since you said this is all c# and you are going for efficiency, you really should get rid of the string concatenation += operators.  Use a StringBuilder, or Console.WriteLine each string directly.  Using string concatenation in a loop is a big performance problem because it allocates new memory on the heap each time you use it.  In other words, s += “foo” needs three separate memory locations (the original value of s, the value of “foo” and the new value of the result).

  • You’re right… thanks for the comment!
    For reference, I’ve added a note to the post.

  • Guest

    As Matt mentioned , the string concatination is the real bottleneck. The solutions are all pretty similar in performance and only differ by the number of result+= statements they use which is why the bitwise version appears faster.

  • lavaeagle

    Just for kicksI wrote this as ternary statements in PHP in one line.  Basically unreadable but funny.

  • jim ji

    if N is a const number, print the result directly is the most efficient way:)

    like this:

    // N is 100.

  • Pingback: The Complete Guide to Nailing Your Next Interview()

  • trevdak

    jim.ji is correct you can’t be a straight-up print.
    While we’re at it though, you can eliminate a lot of the math and if statements except for a bit right at the start, and allow for any N:

    We know that the pattern repeats for every 15 n. So why not just determine the length, then print the repeating string until you get to the necessary length?

  • Eren Yavuz

    private static Dictionary Rules { get; } =
    new Dictionary { { 3, “Fizz” }, { 5, “Buzz” } };

    private static string Convert(int i)
    var stringBuilder = new StringBuilder();

    foreach (var rule in Rules) if (i % rule.Key == 0) stringBuilder.Append(rule.Value);

    return string.IsNullOrEmpty(stringBuilder.ToString()) ? i.ToString() : stringBuilder.ToString();

    private static void Main()
    Print(1, 20);


    private static void Print(int start, int count)
    var collection = Enumerable.Range(start, count).Select(Convert);

    Console.WriteLine(string.Join(Environment.NewLine, collection));