Skip to content
Hoodie for your thoughts: take the Developer Skills Survey for a chance to win free merch Start survey
69% of tech leaders are preparing their teams for GenAI. Uncover more insights in the AI Skills Report. Read now
Adapt your hiring strategy for an AI-powered future. Uncover more insights in our latest whitepaper. Read now
HackerRank Updates

FizzBuzz Code Golf Competition

Written By Harishankaran Karunanidhi | December 20, 2012

FizzBuzz is a simple coding challenge that challenges coders to write the most basic code. Popularized by Jeff Atwood, in FizzBuzz you print the numbers from 1 to 100. But you replace numbers divisible by 3 with “Fizz”, and all other numbers divisible by 5 with “Buzz”.

This past Friday, HackerRank launched a FizzBuzz competition with a twist. Contestants had to solve FizzBuzz with the shortest source code possible. They also had to make each output on a new line. In short, it was a FizzBuzz Code Golf contest.

Code Golf focuses on writing the shortest possible code. While tedious, Code golf helps you really learn the ins and outs of a new language. You need to learn many different features and tricks of a language to master Code Golf, and some of this knowledge is useful when writing real code.

We asked the top scorers in our challenge if they could share their FizzBuzz Code Golf code and approach with others, and many graciously agreed. The top languages used to solve the contest were Perl, Ruby, and Python. Scroll down to see a solution from each language.

Ruby

FizzBuzz is a simple “challenge”. So even a regular Ruby solution only takes 90 characters. The code is quite simple. If a number is divisible by 3 or 5 it will both print the relevant word and set the variable ‘f’ to true. Otherwise, f won’t be initialized, so the 4th line can print the actual number.

The shortest Ruby solution was submitted by Keisuke Nakano (@ksknac) and it uses only 53 characters: The code ‘puts’ the whole output for each line, so no extra call is needed for a new line character. By using some clever modular arithmetic, it eliminates the need to have separate checks to see if n divides by 3 and 5.

Instead, it slices the string to ensure it prints the relevant part, like this: puts’FizzBuzzn’[start,end]. To get the right output, the code makes sure that start equals the following for each n:

  • n = multiple of 3: -9
  • n = multiple of 5: -5
  • n = multiple of 3 and 5: 0
  • none of the above: -14 (So the slice will be nil and it will print the number instead.)

Keisuke accomplished this feat with a variation of Fermat’s Little Theorem. By raising n to a power, you can make sure to get the right remainders for each factor. Thus, for each n, n**4%15 will equal:

  • n = multiple of 3: 6
  • n = multiple of 5: 10
  • n = multiple of both 3 and 5: 0
  • none of the above:  1

Instead of dividing by 15, the code uses -15, to output the previous results (which are 15 less). Those results work out perfectly to print the correct part of the string, or to get nil when necessary. The extra newline doesn’t harm anything since Ruby’s ‘puts’ method only adds a new line when necessary. It’s a complicated solution, but it’s very short code.

Python

The shortest Python solution was submitted by user fR0DDY (though other users also tied with that length). He started by writing some straightforward and concise code.

He then used a shorter form of the ternary operator in python: [falseVal,trueVal][test], so the ‘if’s’ and ‘else’s’ could be eliminated. Since one of the values is an empty string, the expression can be shortened further in Python to trueVal*test, which will result with nothing if the test is false.

This leaves the following code:
for x in range(100):
print ‘Fizz’*(x % 3 == 2) + ‘Buzz’*(x % 5 == 4) or x + 1

The code can then be shortened further by changing the test to return 1 for true and 0 for false. So the first test can be changed to x%3/2 since if x+1 is a multiple of 3, then x%3/2 = 1. Similarly, the second test can be changed to x%5/4 since if x+1 is a multiple of 5, then x%5/4 = 1.

By using a couple more small tricks, the entire code can be shortened to only 55 characters, which is about the same length as the Ruby solution (just clearer!):

You can also view the code on Stypi.

Perl

While Ruby and Python could both be used for short solutions, the top entries all used Perl, a language known for its conciseness. Multiple people submitted almost identical Perl solutions that used only 48 characters!

By some amazing coincidence, this same solution as the winning code also appears on PerlMonks. Despite its brevity, the code is pretty direct, and you can follow the gist of what’s going on without even knowing Perl.

FizzBuzz Code Golf Stats

Perl was clearly the most concise language, but Python and Ruby also put up a tough fight. Other top entries were coded in Javascript, PHP and C. Below is a chart of the average and best scores for each language (from the top 99 submissions). The score formula is: 200 - character length of the code.

FizzBuzz is a very simple problem, but there are many different ways to solve it! The top Ruby solution was the most complicated, while the Python and Perl solutions were more direct.

To see Code Golf solutions in even more languages, check out this blog post. You can also use “normal” FizzBuzz solutions as a simple way to compare different languages. To do this, check out the hundreds of solutions to FizzBuzz on Rosetta Code.

Harishankaran Karunanidhi

q