Inductive Chain Learning With FizzBuzz

Vakas Akhtar
Jul 15, 2020 · 5 min read

Fizz buzz started out as an innocent game to help children learn how to divide numbers. Over time it’s evolved into a test to filter out prospective applicants for various software engineering jobs.

The Fizz-Buzz test was invented by Imran Ghory in 2007 to interview software developers and to find out if the candidate can actually write code. The test is designed to be quite simple. Many still fall victim to it ironically due to its overly simplistic nature. Another reason some may fail is that they became familiar with a certain language by memorizing certain patterns, rather than being aware of the logic used in their programs. With that being said, there’s no need to feel discouraged. Failure is good for programmers as long as they learn from it. Today I’ll be walking through a common Fizz-Buzz problem and hopefully what you take away is not what code I used, but the logic behind the problem. I will be using Ruby to solve this problem but any language can be used to do so and once again, its all about the logic!

The problem is as follows:

(Disclaimer: There are multiple ways to solve this problem. Take a moment to think about how you would approach it before looking at the solution.)

Let’s list what this problem is asking us to do:

  1. Create a loop that counts up to 100 and ends if the number is over 100.
  2. If the number is divisible by 3 it returns “Fizz”
  3. If the number is divisible by 5 it returns “Buzz”
  4. If the number is divisible by both 3 and 5 it returns “FizzBuzz”

Ok let’s get started! Take a look at the code below.

Great! Now we have a loop set up that will count from 1 to 100. The upto method is a convenience method that does the same thing as a for loop.

Here, I set an empty string equal to the variable str to collect all the elements and puts str will output the final result. Remember, our goal is to output the strings “Fizz”, “Buzz”, and “FizzBuzz” when a number meets the requirements from the problem. The program will keep adding on to the empty string as it goes through the logic.

Be sure to include .to_s since adding an integer to a string would result in an error

Now we have our logic inserted to the loop. The variable str will receive either the “Fizz” or “Buzz” with the help of the += assignment operator. We wouldn’t use the shovel method << in this case since it alters the original string rather than creating a new one. The modulo operator (%) is used to determine whether or not the number in question is divisible by 3 or 5. A modulo operation finds the remainder or signed remainder after division of one number by another. For example,

21/7 = 3.0 
22/7 = 3.14285714286

It’s imperative that you understand how modulo operations work, without it you would lack the ability to solve similar problems in the future.

Now when you run the file and check the results you should get…

Congratulations! Now you’ll be more than ready when your interviewer asks you to solve their FizzBuzz problem.

Project Euler

Now that you’re comfortable with FizzBuzz let’s take a look at another problem that’s quite similar. We’ll be looking at the first problem from

Leonhard Euler was a Swiss mathematician, physicist, astronomer, geographer, logician, and engineer states that it is designed to challenge one’s problem solving and mathematics skills. A mathematics background is not necessary to complete the problems however, there are over 700 problems that get more difficult as you progress. The interesting part is that it involves inductive chain learning, meaning that by solving one problem it will expose you to a new concept that allows you to solve a previously inaccessible problem. Now let’s take a look at the first problem.

Wow! Would you look at that, it looks pretty similar to the FizzBuzz problem we did before. It seems our inductive chain learning process already started before we even embarked on our project Euler Journey!

Let’s begin:

Similar to the last problem, it’s asking us to print out a list of numbers and pull out the ones that are divisible by 3 or 5. This time instead of 1 to 100 it is asking for all numbers below 1000.

First we have to set the variable total = 0 to allow us to store all elements that are divisible by 3 or 5. Later on we will be able to call on the variable to add up the elements and return the sum.

Now we code in our if statement with the modulo operations that will collect all the necessary numbers. Then by using the assignment operator += we add the iterated numbers onto the total where it is then finally outputted.

Now lets run the code and see what our answer is:

Here’s the answer on

Well 1 problem down and over 700 more to go ¯\_(ツ)_/¯. Just kidding, to be quite honest completing all the problems would be an incredible feat but just completing around 50 Project Euler problems would be enough to build up your computational logic. Don’t forget to push up your solutions to GitHub as well!

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store